简体   繁体   English

CSRF令牌丢失或在Django中不正确

[英]CSRF token missing or incorrect in django

views.py views.py

from django.shortcuts import render
from django.contrib.auth.decorators import login_required
from django.http import HttpResponse
import MySQLdb
from django.shortcuts import render_to_response
from django.shortcuts import HttpResponseRedirect
from django.template.loader import get_template
from django.template import Context, Template,RequestContext
import datetime
import hashlib
from random import randint
import random
from django.views.decorators.csrf import csrf_protect, csrf_exempt
from django.template.context_processors import csrf
import requests
from django.template import RequestContext
from log.forms import *

@csrf_protect
def register(request):
    if request.method == 'POST':
        form = RegistrationForm(request.POST)
        if form.is_valid():
            user = User.objects.create_user(
            username=form.cleaned_data['username'],
            password=form.cleaned_data['password1'],
            email=form.cleaned_data['email']
            )
            return HttpResponseRedirect('/register/success/')
    else:
        form = RegistrationForm()
    variables = RequestContext(request, {
    'form': form
    })

    return render_to_response(
    'register.html',
    variables,
    )
   # return render(request,"recharge.html")
def register_success(request):
    return render_to_response(
    'registration/success.html',
    )
 base.html 
 <form method="post" action="."> {% csrf_token %}
        <table border="0"> 
                { form.as_table }}
        </table>
    <button type="submit" value="Register">Register</button>
    <button type="button" onclick="window.location.href='/' ">Login</button>
 </form>

register.html register.html

<!-- register.html -->
{% extends "base.html" %}
{% block title %}User Registration{% endblock %}
{% block head %}User Registration{% endblock %}
{% block content %}
    <form method="post" action=".">{% csrf_token %}
        <table border="0">
            {{ form.as_table }}
        </table>
        <button type="submit" value="Register">Register</button>
        <button type="button" onclick="window.location.href='/' ">Login</button>
    </form>
{% endblock %}

settings.py settings.py

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [os.path.join(BASE_DIR, 'templates')],
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
            ],
        },
    },
]

My django version: 1.10.4 我的Django版本:1.10.4

How can i solve this problem? 我怎么解决这个问题?

call render_to_response like this: 像这样调用render_to_response

from django.template import RequestContext

@csrf_protect
def register(request):
    # ...

    return render_to_response(
        'register.html',
        {'form': form}, 
        RequestContext(request)
    )

or just use render shortcut method 只使用渲染快捷方式

@csrf_protect
def register(request):
    # ...
    return render(request, 'register.html', {'form': form})

from 1.8 docs : 1.8 docs开始

render() is the same as a call to render_to_response() with a context_instance argument that forces the use of a RequestContext. render()与带有context_instance参数的render_to_response()调用相同,该参数强制使用RequestContext。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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