0

新しいページへのリダイレクトがエラーで失敗しています。

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
4

1 に答える 1

5

これは非常に明確だと思います:

失敗の理由: CSRF Coo​​kie が設定されていません。

テンプレートには、内部 URL を対象とする各 POST フォーム内に {% csrf_token %} テンプレート タグがあります。

CSRFフォームにトークンを含める必要があります。

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

<form method="post" action=".">{% csrf_token %}
于 2012-08-04T18:19:03.287 に答える