码迷,mamicode.com
首页 > 其他好文 > 详细

Django 表单 POST CSRF verification failed. Request aborted.错误

时间:2015-07-16 21:17:15      阅读:107      评论:0      收藏:0      [点我收藏+]

标签:

这里有答案

http://www.qttc.net/201209211.html

 

总结就是

1.在模板中要提交的form中加入{% csrf_token %}

<form method="post">
  {% csrf_token %}
    {{form.as_p}}
    <input type="submit" name="ok">
</form>

2.把views中的

  render_to_response(‘h1.html‘,{‘form‘:form})

改成

  render_to_response(‘h1.html‘,{‘form‘:form},context_instance=RequestContext(request))

from django.shortcuts import render_to_response, RequestContext
from django.http import HttpResponse
from appA.models import *
from appA.forms import *

def hello(request):
    if request.method ==POST:
        form = Mybook(request.POST)
        if form.is_valid():
            data = form.cleaned_data
            name = data[name]
        return HttpResponse(name)

    form = Mybook()
    return render_to_response(h1.html,{form:form},context_instance=RequestContext(request))

 

Django 表单 POST CSRF verification failed. Request aborted.错误

标签:

原文地址:http://www.cnblogs.com/JamesZhangXX/p/4652099.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!