Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django 1.8 - KeyError 'request'

I'm a bit dumbfounded here, hopefully someone out there understands this issue!

This is the context:

{'form': <LoginForm bound=False, valid=Unknown, fields=(password;remember;login)>,
 'redirect_field_name': 'next',
 'redirect_field_value': None,
 'signup_url': u'/accounts/signup/',
 'site': <Site: brilliantactor.com>,
 u'view': <allauth.account.views.LoginView object at 0x10d7dead0>}

The request object looks pretty normal

'<WSGIRequest\npath:/accounts/login/,\nGET:<QueryDict: {}>,\nPOST:<QueryDict: {}>,\nCOOKIES:{\'_ga\': \'GA1.1.908939259.1424705622\',\n \'csrftoken\': \'Ga0urMmd7AgBouS9KeH5V4EQNoyE8cqU\',\n [...]

But when the following line is read:

context = make_context(context, request)

The output context is as follows

[{'False': False, 'None': None, 'True': True}, 
 {}, 
 {'form': <LoginForm bound=False, valid=Unknown, fields=(password;remember;login)>, 
  'redirect_field_value': None, 
  'redirect_field_name': 'next', 
  'signup_url': u'/accounts/signup/', 
  'site': <Site: brilliantactor.com>, 
  u'view': <allauth.account.views.LoginView object at 0x10d7dead0>}]

As the new context object has no 'request' key, a few templatetags fail e.g. django-allauth


Here is an example of were it fails:

https://github.com/pennersr/django-allauth/blob/master/allauth/socialaccount/templatetags/socialaccount.py#L20


My TEMPLATE_CONTEXT_PROCESSORS:

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [
            join(BASE_DIR, 'templates'),
            # insert your TEMPLATE_DIRS here
        ],
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                # Insert your TEMPLATE_CONTEXT_PROCESSORS here or use this
                # list if you haven't customized them:
                'django.contrib.auth.context_processors.auth',
                'django.template.context_processors.debug',
                'django.template.context_processors.i18n',
                'django.template.context_processors.media',
                'django.template.context_processors.static',
                'django.template.context_processors.tz',
                'django.contrib.messages.context_processors.messages',

                "allauth.account.context_processors.account",
                "allauth.socialaccount.context_processors.socialaccount",
            ],
        },
    },
]

Has anyone seen this before?

like image 765
RadiantHex Avatar asked Mar 27 '15 12:03

RadiantHex


People also ask

What is KeyError in Django?

The Python KeyError is a type of LookupError exception and denotes that there was an issue retrieving the key you were looking for. When you see a KeyError , the semantic meaning is that the key being looked for could not be found.

How a request is processed in Django?

Django uses request and response objects to pass state through the system. When a page is requested, Django creates an HttpRequest object that contains metadata about the request. Then Django loads the appropriate view, passing the HttpRequest as the first argument to the view function.

What is HttpResponse in Django?

HttpResponse Methods – Django It is used to instantiate an HttpResponse object with the given page content and content type. HttpResponse.__setitem__(header, value) It is used to set the given header name to the given value.


2 Answers

For 1.8, add

'django.template.context_processors.request',

instead of

'django.core.context_processors.request',
like image 98
Ramkishore M Avatar answered Oct 11 '22 16:10

Ramkishore M


As Alex hints, you need to add the request context processor; it's not activated by default.

'django.core.context_processors.request',
like image 41
Daniel Roseman Avatar answered Oct 11 '22 17:10

Daniel Roseman