除了使用Django内置表单,有时往往我们需要自定义表单。对于自定义表单Post方式提交往往会带来由CSRF(跨站请求伪造)产生的错误\”CSRF verification failed. Request aborted.\”
本篇文章主要针对\”表单提交\”和\”Ajax提交\”两种方式来解决CSRF带来的错误
一、表单提交
Template:
计算数字和
Views.py:
def Calculate(request): if request.POST: a=request.POST[\"ValueA\"] b=request.POST[\"ValueB\"] c=str(int(a)+int(b)) return render_to_response(\'Result.html\',{\'result\':c}) else: return render_to_response(\'Calculation.html\',context_instance=RequestContext(request))
需要注意:
(1)在