0
我還有一個問題是由於我對Django應用程序進行的主要升級(從1.7到1.10和Django Rest Framework到3.5.4)而導致的。我設法修復了一些在註冊頁面上導致500錯誤的錯誤,但是我仍然遇到了CSRF令牌的問題。升級之前,此功能正在運行。我檢查了全部頁提到的錯誤的東西(我有DEBUG = TRUE),但他們似乎確定,爲遵循:Django CSRF令牌從註冊表中丟失
在類「CompanyAdmin」我有註冊方法:
def signup(self, request):
if request.method == "GET":
form = SignupForm()
else:
form = SignupForm(request.POST)
if form.is_valid():
company = form.save()
user = company.managers.all()[0]
user = authenticate(
email=user.email, password=request.POST["password1"])
if user is not None:
login(request, user)
return HttpResponseRedirect(reverse("bagdisken:index"))
return shortcuts.render_to_response("bagdisken/signup.html",
context=RequestContext(request, {
'form': form,
}))
我在上下文中遇到了問題,因此我通過在上下文中添加請求來解決500錯誤。以前這是這樣的:
return shortcuts.render_to_response("bagdisken/signup.html", {'form': form})
的CSRF令牌存在於視圖:
{% extends "admin/base_site.html"%}
{% load form_utils %}
{% block content_title%}{% endblock %}
{% block breadcrumbs %}{% endblock %}
{% block nav-global %}{% endblock %}
{% block coltype %}twelve columns offset-by-two{% endblock %}
{% block content%}
<form id="signup" action="#" method="POST" >
{% csrf_token %}
{{ form|render }}
<input type="submit" value="Submit" />
</form>
{% endblock %}
而且CsrfViewMiddleware也存在於設置:
MIDDLEWARE_CLASSES = (
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'corsheaders.middleware.CorsMiddleware',
# Uncomment the next line for simple clickjacking protection:
# 'django.middleware.clickjacking.XFrameOptionsMiddleware',
)
儘管如此,隱藏的輸入因爲csrf標記不存在。我該如何解決這個問題?
工作就像一個魅力,謝謝你! –