Question

I'm using the django_browserid app. Here is part of my settings file.

from django.conf.global_settings import TEMPLATE_CONTEXT_PROCESSORS as TCP
TEMPLATE_CONTEXT_PROCESSORS = TCP + (
    "django.core.context_processors.request",
    'django_browserid.context_processors.browserid',
)

This all works fine when DEBUG = True. In production mode it doesn't work for one reason or another. Part of the stack trace:

return context['browserid_css'](**kwargs)
KeyError: 'browserid_css'

So it looks like the context processor isn't running. That's my guess. The only difference between the working and failing is DEBUG=True/False.

If I debug, the value of context in debug mode is what you would expect:

[{'False': False, 'None': None, 'True': True}, [{'False': False, 'None': None, 'True': True}, {u'csrf_token': ... etc

and in production mode:

[{'False': False, 'None': None, 'True': True}, {}]

It looks like the request is missing from the context. It also looks like this is happening when the template is loaded, not part of the request, so it's not surprising there is no request context.

Why does BrowserId only work in debug mode?

Was it helpful?

Solution

Interesting debugging session. Answering here because it's sure to happen to someone else.

The full stack trace was this:

  File "/Users/joe/Envs/folktunefinder/lib/python2.7/site-packages/django/views/defaults.py", line 41, in server_error
    return http.HttpResponseServerError(template.render(Context({})))
  File "/Users/joe/Envs/folktunefinder/lib/python2.7/site-packages/django/template/base.py", line 140, in render
    return self._render(context)
  File "/Users/joe/Envs/folktunefinder/lib/python2.7/site-packages/django/template/base.py", line 134, in _render
    return self.nodelist.render(context)
  File "/Users/joe/Envs/folktunefinder/lib/python2.7/site-packages/django/template/base.py", line 830, in render
    bit = self.render_node(node, context)
  File "/Users/joe/Envs/folktunefinder/lib/python2.7/site-packages/django/template/base.py", line 844, in render_node
    return node.render(context)
  File "/Users/joe/Envs/folktunefinder/lib/python2.7/site-packages/django/template/loader_tags.py", line 124, in render
    return compiled_parent._render(context)
  File "/Users/joe/Envs/folktunefinder/lib/python2.7/site-packages/django/template/base.py", line 134, in _render
    return self.nodelist.render(context)
  File "/Users/joe/Envs/folktunefinder/lib/python2.7/site-packages/django/template/base.py", line 830, in render
    bit = self.render_node(node, context)
  File "/Users/joe/Envs/folktunefinder/lib/python2.7/site-packages/django/template/base.py", line 844, in render_node
    return node.render(context)
  File "/Users/joe/Envs/folktunefinder/lib/python2.7/site-packages/fancy_tag/__init__.py", line 101, in render
    return func(*args, **kwargs)
  File "/Users/joe/Envs/folktunefinder/lib/python2.7/site-packages/django_browserid/templatetags/browserid.py", line 30, in browserid_css
    return context['browserid_css'](**kwargs)
  File "/Users/joe/Envs/folktunefinder/lib/python2.7/site-packages/django/template/context.py", line 57, in __getitem__
    raise KeyError(key)
KeyError: 'browserid_css'

After reconfiguring logging, it looks like I was getting an error:

SuspiciousOperation: Invalid HTTP_HOST header (you may need to set ALLOWED_HOSTS): localhost:8000

This ALLOWED_HOSTS setting only applies in production mode. Django was trying to render this error and somehow rendering a template that included a BrowserID tag without a request context.

So if you get this error, first check ALLOWED_HOSTS.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top