Forbidden (403) CSRF verification failed. Request aborted.

來源:互聯網
上載者:User

  學習django1.4,沒有中文資料,只能慢慢啃官方教程。

  最近被這個問題糾結幾天了,一直沒有解決,雖然用的是業餘時間,但是這個問題不解決,後面的教程感覺都做不下去了。這個問題是這樣長生的:我用post方法提交表單,出現標題那個錯誤,django錯誤系統也給出了三個途徑,除了第三個沒有完全懂之外,其它的都試了好多遍了。最後卻發現在setting裡面把CSRF的注釋掉就正確了。還是把django的提示貼出來吧(不過沒有用哦):

  

In general, this can occur when there is a genuine Cross Site Request Forgery, or when Django's CSRF mechanism has not been used correctly. For POST forms, you need to ensure:Your browser is accepting cookies.The view function uses RequestContext for the template, instead of Context.In the template, there is a {% csrf_token %} template tag inside each POST form that targets an internal URL.If you are not using CsrfViewMiddleware, then you must use csrf_protect on any views that use the csrf_token template tag, as well as those that accept the POST data.

  最後成功的解決辦法:在setting.py修改如下。

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',)

  這是我自學python的第一個文章吧,也算是開了第一篇了,雖然簡單,自己感覺還是蠻艱難的。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.