Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

django csrf RequestContext

If I include {% csrf_token%} in my form template and import RequestContext in my view,

do I have to include anything else in my view or will the csrf protection be taken care of just be the following:

from django.shortcuts import render_to_response
from django import forms
from django.http import HttpResponseRedirect
from django.template import Template, RequestContext
from dash.forms import GradeForm


def register(request):
    if request.method == 'POST':
        form = GradeForm(data=request.POST)
        if form.is_valid():
            new_dash_profile = form.save()
            new_user = form.save()
            return  HttpResponseRedirect("/success/")
        else:
            form = RegisterForm()
        return render_to_response('grade.html',{'form':form})
like image 781
Eva611 Avatar asked Apr 17 '11 05:04

Eva611


1 Answers

To me, the easiest way is to add a RequestContext to the render_to_response function

return render_to_response('grade.html',
                          {'form':form},
                          context_instance=RequestContext(request))

This is just one possibility, the important thing is that you should process the csrf token somewhere, and RequestContext does that.

An other possibility is to do ir manually:

from django.template.context_processors import csrf

params = {}
params.update(csrf(request))
return render_to_response('grade.html', params)
like image 135
fceruti Avatar answered Oct 30 '22 19:10

fceruti