gpt4 book ai didi

python - Django 中的 CSRF token 丢失或不正确

转载 作者:行者123 更新时间:2023-11-28 21:43:35 25 4
gpt4 key购买 nike

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 -->
{% 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

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',
],
},
},
]

我的django版本:1.10.4

我该如何解决这个问题?

最佳答案

像这样调用 render_to_response:

from django.template import RequestContext

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

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

或者只使用渲染快捷方式

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

来自 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.

关于python - Django 中的 CSRF token 丢失或不正确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41976153/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com