开发者

django csrf RequestContext

If I include {% csrf_token%} in my form template and impor开发者_JS百科t 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})


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)
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜