Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django not able to render context when in shell

This is what I am trying to run. When I run the server and run these lines within a view and then return an HttpResponse, then everything goes fine. However when I run python manage.py shell and then try to run through these lines then 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/'

Also, I am using django 1.8.

like image 222
Chase Roberts Avatar asked Apr 10 '15 22:04

Chase Roberts


1 Answers

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

Meanwhile, you can manually activate a language in your shell to fix it:

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

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

like image 151
knbk Avatar answered Nov 01 '22 00:11

knbk