CSRF verification failed. Request aborted

来源:互联网 发布:绝世武神坐骑进阶数据 编辑:程序博客网 时间:2024/05/16 13:12

环境:Django 1.11


在Form中使用CSRF的过程当中,报了一个CSRF verification failed. Request aborted错误。搜索了下大部分的解决方案都是说在settings中设置csrf的配置,理论上这个配置是默认使用,故认为不是这个原因。


http://blog.csdn.net/cq361106306/article/details/42494189 中提到了的方法把和POST页面相关的所有view的context强制换成RequestContext。代码范例如下:


return render_to_response('wind/index.html',context,context_instance=RequestContext(request) )  


结果发现问题依然没有解决,甚至出现了Django error: render_to_response() got an unexpected keyword argument 'context_instance'错误。

查找文档后发现,自Django1.8之后,context_instance已经不再被鼓励使用,并从1.10之后的版本中移除。争取的解决办法应该是使用render方法渲染前端页面

from django.shortcuts import renderdef my_view(request):    context = {'foo': 'bar'}    return render(request, 'my_template.html', context)

再次运行project,问题解决

阅读全文
0 0
原创粉丝点击