Django cannot display context when in shell

This is what I'm trying to run. When I start the server and run these lines in the view and then return the HttpResponse, everything goes well. However, when I run python manage.py shell and then try to execute these lines, I get an error:

 product = Product.objects.get(pk=4) template = loader.get_template('weekly-email.html') user = User.objects.get(pk=1) body = template.render(Context({ 'user': user, 'product': product, })) 

Output:

 Traceback (most recent call last): File "<console>", line 1, in <module> File "/Users/croberts/.virtualenvs/testproj/lib/python3.4/site-packages/django/template/backends/django.py", line 74, in render return self.template.render(context) File "/Users/croberts/.virtualenvs/testproj/lib/python3.4/site-packages/django/template/base.py", line 209, in render return self._render(context) File "/Users/croberts/.virtualenvs/testproj/lib/python3.4/site-packages/django/template/base.py", line 201, in _render return self.nodelist.render(context) File "/Users/croberts/.virtualenvs/testproj/lib/python3.4/site-packages/django/template/base.py", line 903, in render bit = self.render_node(node, context) File "/Users/croberts/.virtualenvs/testproj/lib/python3.4/site-packages/django/template/base.py", line 917, in render_node return node.render(context) File "/Users/croberts/.virtualenvs/testproj/lib/python3.4/site-packages/django/template/base.py", line 963, in render return render_value_in_context(output, context) File "/Users/croberts/.virtualenvs/testproj/lib/python3.4/site-packages/django/template/base.py", line 939, in render_value_in_context value = localize(value, use_l10n=context.use_l10n) File "/Users/croberts/.virtualenvs/testproj/lib/python3.4/site-packages/django/utils/formats.py", line 181, in localize return number_format(value, use_l10n=use_l10n) File "/Users/croberts/.virtualenvs/testproj/lib/python3.4/site-packages/django/utils/formats.py", line 162, in number_format get_format('DECIMAL_SEPARATOR', lang, use_l10n=use_l10n), File "/Users/croberts/.virtualenvs/testproj/lib/python3.4/site-packages/django/utils/formats.py", line 110, in get_format for module in get_format_modules(lang): File "/Users/croberts/.virtualenvs/testproj/lib/python3.4/site-packages/django/utils/formats.py", line 82, in get_format_modules modules = _format_modules_cache.setdefault(lang, list(iter_format_modules(lang, settings.FORMAT_MODULE_PATH))) File "/Users/croberts/.virtualenvs/testproj/lib/python3.4/site-packages/django/utils/formats.py", line 51, in iter_format_modules if not check_for_language(lang): File "/Users/croberts/.virtualenvs/testproj/lib/python3.4/site-packages/django/utils/translation/__init__.py", line 181, in check_for_language return _trans.check_for_language(lang_code) File "/Users/croberts/.virtualenvs/testproj/lib/python3.4/functools.py", line 472, in wrapper result = user_function(*args, **kwds) File "/Users/croberts/.virtualenvs/testproj/lib/python3.4/site-packages/django/utils/translation/trans_real.py", line 409, in check_for_language if not language_code_re.search(lang_code): TypeError: expected string or buffer 

edit : and here is my settings.py:

 # Build paths inside the project like this: os.path.join(BASE_DIR, ...) import os DEBUG = True BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) SECRET_KEY = 'SECRET' ALLOWED_HOSTS = [] AUTH_USER_MODEL = 'crunch.User' STATICFILES_DIRS = ( '/Users/croberts/testproj/static/', ) # Application definition INSTALLED_APPS = ( 'django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', 'crunch', 'emailmanager', ) MIDDLEWARE_CLASSES = ( 'django.contrib.sessions.middleware.SessionMiddleware', 'django.middleware.common.CommonMiddleware', 'django.middleware.csrf.CsrfViewMiddleware', 'django.contrib.auth.middleware.AuthenticationMiddleware', 'django.contrib.auth.middleware.SessionAuthenticationMiddleware', 'django.contrib.messages.middleware.MessageMiddleware', 'django.middleware.clickjacking.XFrameOptionsMiddleware', 'django.middleware.security.SecurityMiddleware', ) ROOT_URLCONF = 'testproj.urls' WSGI_APPLICATION = 'testproj.wsgi.application' DATABASES = { 'default': { 'ENGINE': 'django.db.backends.sqlite3', 'NAME': os.path.join(BASE_DIR, 'database'), } } LANGUAGE_CODE = 'en-us' TIME_ZONE = 'MST' USE_I18N = True USE_L10N = True USE_TZ = False STATIC_URL = '/static/' MEDIA_ROOT = BASE_DIR+'/media/' MEDIA_URL = '/media/' 

In addition, I am using django 1.8.

+6
source share
1 answer

This is a known issue and will be fixed in 1.8.1.

In the meantime, you can manually activate the language in your shell to fix it:

 from django.utils.translation import activate activate('en') # or any language code 

UPDATE : version 1.8.1 was released, so the best solution is to upgrade to the latest version 1.8.x.

+10
source

Source: https://habr.com/ru/post/984969/


All Articles