學習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的第一個文章吧,也算是開了第一篇了,雖然簡單,自己感覺還是蠻艱難的。