2

I have been trying to use the updated yourlabs subscription example, and the installation has worked fine except that

  • Each of the views in the subscription app returns the request variable in the render to template.

  • this request context variable is not received in the template and as a result I am getting the following error

    Caught VariableDoesNotExist while rendering: Failed lookup for key [request] in u

Since this is being returned in every view I can't solve this problem by making some tweaks in a particular template

Sachin
  • 3,672
  • 9
  • 55
  • 96
  • The views in yourlabs-subscription use shortcuts.render and do not pass the request object in the context. It passes the request object to django.shortcut.render so that it can run context processors. django.core.context_processors.request is the context processor that sets request in the context. – jpic Jan 16 '12 at 15:39

1 Answers1

2

This is happening because request isn't in your template's context and the template is using some template code that expected it to be there. That code (e.g. a custom template tag) should better handle VariableDoesNotExist

In addition, your views probably shouldn't return request in every response explicitly. Let Django handle this for you.

To do this, add the request template context processor that ships with Django to your TEMPLATE_CONTEXT_PROCESSORS:

TEMPLATE_CONTEXT_PROCESSORS = (
    ...
    'django.core.context_processors.request',
    ...
)

If you are already using this template context processor, ensure that render_to_response is called with context_instance=RequestContext(request) as the final argument (the below example is from the docs):

def some_view(request):
# ...
return render_to_response('my_template.html',
                          my_data_dictionary,
                          context_instance=RequestContext(request))

This ensures that all the dicts returned by the template context processors in TEMPLATE_CONTEXT_PROCESSORS are passed to the template.

You could also use the rendershortcut, which will automatically render the template with an instance of Requestcontext.

Another option added in Django 1.3 is the TemplateResponse, which will also use an instance of RequestContext.

Ryan Kaskel
  • 4,744
  • 1
  • 19
  • 18