Question

I am having an issue with any view that calls context.update(csrf(request)) to set csrf on pages called via POST. This issue basically is that I can't use any custom context processors or ones like {% if user.is_authenticated %}

I am using RequestContext(request) on render_to_response

Any ideas on to resolve this issue?

if not request.POST:
    return HttpResponseRedirect('/')
searchpilotidform = searchpilotid()
searchcallsignform = searchcallsign()
searchairportform = searchairport()
searchairportpairform = searchairportpair()

searchpilotnameform = searchpilotname()
pilots = cid.objects.filter(realname__icontains=request.POST['name'])
context = {'searchairportpairform': searchairportpairform, 'searchpilotnameform': searchpilotnameform, 'searchpilotidform': searchpilotidform, 'query': request.POST['name'], 'pilots': pilots, 'searchcallsignform': searchcallsignform, 'searchairportform': searchairportform}
context.update(csrf(request))
return render_to_response('pilotnamesearch.html', context, content_type=RequestContext(request))
Was it helpful?

Solution

You're using the wrong parameter to render_to_response. It should be context_instance=RequestContext(request).

Since you weren't really using a RequestContext, none of the context processors would work. Once you replace it, there'll no longer be any need to add the CSRF token manually.

content_type is something else entirely - the MIME type of the response. Since it's not expecting a RequestContext instance, it's a bit surprising this does not raise an error.

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