Forbidden (403) CSRF verification failed. Request aborted. 解决之道

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/zgyulongfei/article/details/8830750

博客转载请注明出处!

原文链接:http://blog.csdn.net/zgyulongfei/article/details/8830750


先让我还原一下错误页面:



今天被这个错误折腾了一上午,搞得我心力交瘁。网上搜索了很多答案,大致解决方案都是相同,我照着做,可是错误依然存在。

后来找jack帮忙才总算解决了这个问题。

原来我之前的方案一直是本末倒置,没有摸清楚django的csrf原理,就在那边瞎弄,浪费了很多时间。

下面我来说说我问题所在,如果你和我犯了同样的错误,可以做一下参考。


首先我做了一个test.html,如下:

<!DOCTYPE>
<html lang="zh-CN">
    
    <head>
        <meta charset="utf-8">
        <title>
            test
        </title>
    </head>
    
    <body>
        <form action="/postblog" method="post">
            {% csrf_token %}
            <input type="text" name="blog" />
            <input type="submit" value="发布" />
        </form>
    </body>

</html>

访问时候为 xx.xx.com/post,执行的方法为

def post_html(rq):
	return render_to_response("test.html") 

这个表单中已经包含了tag {% csrf_token %},这是网上解决方案中都说需要加上的标签。

点击【发布】按钮提交表单之后,执行/postblog操作,接下来看看服务端代码。

先在settings.py中加上

django.middleware.csrf.CsrfViewMiddleware

MIDDLEWARE_CLASSES = (
    'django.middleware.common.CommonMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    # Uncomment the next line for simple clickjacking protection:
    # 'django.middleware.clickjacking.XFrameOptionsMiddleware',
)

再看urls.py

 url(r'^postblog$','postBlog')

然后看views.py

def postBlog(rq):
	return render_to_response('ok.html', context_instance=RequestContext(rq))

对于一个django初学者来说,一切都按照错误页面的Help中来做的啊,为什么结果还是错误的呢?

答:因为,,,从一开始的html页面就错了,而我在错误的前提下去做结果的补救,就救不回来了←_←


有经验的django开发者应该一眼就看得出错误的根源。

其实表单中的{% csrf_token %}在html页面被加载的时候就已经被赋值了,而post的时候需要将这个被赋值的csrf_token发送给服务端进行验证,如果验证成功才能进行post操作,否则就被认为是csrf攻击。

而上文中的test.html中一开始并没有由服务器给出一个csrf_token,在post的时候自然而然就不能成功验证。

而 context_instance=RequestContext(rq)的作用就是给这个csrf_token进行赋值,上文中却把赋值的操作在post之后进行,而正确地操作是在post之前打开html页面的时候就赋值。


所以只要修改post_html方法就行:

def post_html(rq):
	return render_to_response("test.html",context_instance=RequestContext(rq)) 

以上。


猜你喜欢

转载自blog.csdn.net/zgyulongfei/article/details/8830750
今日推荐