2012-08-04 122 views
0

我重定向到一個新的頁面與一個錯誤而失敗,Django的重定向失敗

的login.html,

<html> 
    <head> 
     <title>Django Bookmarks -User Login</title> 
    </head> 
    <body> 
     <h1>User Login</h1> 
     {% if form.errors %} 
      <p>Your username and password didn't match</p> 
     {% endif %} 
     <form method="post" action="."> 
      <p><label for="id_username">Username:</label> 
       {{ form.username }}</p> 
      <p><label for="id_password">Password</label> 
       {{ form.password }}</p> 
      <input type="hidden" name="text" value="/" /> 
      <input type="submit" value="login" /> 
     </form> 
    </body> 
</html> 

urls.py

from django.conf.urls.defaults import patterns, include, url 
from bookmarks.views import * 

urlpatterns = patterns('', 
    (r'^$',main_page), 
    (r'^user/(\w+)/$',user_page), 
    (r'^login/$','django.contrib.auth.views.login'), 

) 

錯誤

Error: 

Forbidden (403) 

CSRF verification failed. Request aborted. 
Help 

Reason given for failure: 
    CSRF cookie not set. 

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: 
•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. 

settings.py文件

LOGIN_URL ='/login/' 

LOGIN_REDIRECT_URL = '/login/' 

新的錯誤:

Page not found (404) 



Request Method: 

POST 



Request URL: 

http://127.0.0.1:8000/login/=%22.%22 



Using the URLconf defined in django_bookmarks.urls, Django tried these URL patterns, in this order: 
1. ^$ 
2. ^user/(\w+)/$ 
3. ^login/$ 

The current URL, login/=".", didn't match any of these 

回答

5

我想,這使得它非常清楚:

Reason given for failure: CSRF cookie not set.

In the template, there is a {% csrf_token %} template tag inside each POST form that targets an internal URL.

您需要incude一個CSRF令牌與您的形式:

https://docs.djangoproject.com/en/dev/ref/contrib/csrf/

<form method="post" action=".">{% csrf_token %} 
+0

這是我得到現在---找不到網頁錯誤(404) 請求方法: POST 請求URL: http://127.0.0.1:8000/login/ =%22.%22 – user1050619 2012-08-04 19:40:01

+0

這是一個不同的問題。如果需要,你需要發佈一個單獨的問題。看起來它將您重定向到一個不存在的頁面 – 2012-08-04 20:33:00