简体   繁体   中英

CSRF verification failed. Request aborted. Django 1.7

I'm newbie in django and in web development. I'm trying to build simple website. I'm trying to use POST form. But I'm taking a "Forbidden 403, CSRF verification failed. Request aborted". I walked around a lot of articles, but nothing happens. Tell me what I'm doing wrong, please.

this is my urls.py:

from django.conf.urls import patterns, include, url
from django.contrib import admin
from myapp import views

urlpatterns = patterns('',
    url(r'^admin/', include(admin.site.urls)),
    url(r'^$', views.index, name = "index"),
    url(r'^signup/', views.signup, name = "signup"),
    url(r'^register/', views.register, name = "register"),
)

views.py

from django.shortcuts import render_to_response
from django.http import HttpResponse

def index(request):
    return render_to_response("index.html")

def signup(request):
    return render_to_response("signup.html")

def register(request):
    return HttpResponse("It worked!")

a template where I use form: signup.html

<html>
<head>
    <meta charset = "utf-8">
    <title> Регистрация </title>
    <link rel = "stylesheet", type = "text/css", href = "{% static    'myapp/style.css' %}">
</head>
<body>
    <div class = "gradient"> 
        <table border = "0">
            <tr>
                <td> <h1 class = "headerMargin"> MySite.com </h1> </td>
                <td align = "right", width = "100%"> <button class = "btns">  Вход </button> </td>
            </tr>
        </table>
    </div>
    <form action = "/register/", method = "post">
    <table id = "registerArea", align = "center", border = "0", cellpadding = "10">
        <tr>
            <td> <h2> Регистрация </h2> </td>
        </tr>
        <tr>
            <td>
                <div> <b> Имя пользователя: </b></div>
                <input type = "text", size = "40"> </td>
        </tr>
        <tr>
            <td>
                <div> <b> Электронная почта: </b> </div>
                <input type = "text", size = "40"> </td>
        </tr>
        <tr>
            <td>
                <div> <b> Пароль: </b> </div>
                <input type = "password", size = "40"> </td>
        </tr>
        <tr>
            <td>
                <button id = "btnRegister", type = "submit"> <b> register  </b> </button> </td>
        </tr>
    </table>
    </form>
    <div id = "footer">
        <table align = "center">
            <tr>
                <td> <h4> О нас </h4> </td>
                <td> <h4 class = "line"> Помощь </h4> </td>
                <td> <h4 class = "line"> Правила </h4> </td>
        </table>
    </div>
</body>
</html>

Add {% csrf_token %} template tag inside the <form> tag:

<form action = "/register/" method = "post">
    {% csrf_token %}
    ...
</form>

Documentation is here .

Also you should provide RequestContext to render_to_response . Or use render() :

from django.shortcuts import render

def signup(request):
    return render(request, "signup.html")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM