姜戈菜鸟在这里!我基本上已经在线尝试了所有解决方案,但仍然有错误(一个 Chrome)“CSRF 令牌丢失或不正确”,而 Opera 和 Firefox 返回“CSRF cookie 未设置”...?这是我的文件:
视图.py
# views.py
from django.shortcuts import render_to_response
from django.http import HttpResponse, HttpResponseRedirect
from django.contrib.auth import authenticate, login
from django.template import RequestContext
from django.core.context_processors import csrf
def dashboard(request):
state = "log in"
if request.user.is_authenticated():
return render_to_response('memberbrd.html')
elif request.method == "POST":
username = request.POST.get('username')
password = request.POST.get('password')
user = authenticate(username=username, password=password)
if user is not None:
if user.is_active:
login(request, user)
return HttpResponseRedirect('/')
else:
error = "inactive"
else:
error = "wrong username or password"
render_to_response('visitorbrd.html', {'errors': error}, context_instance = RequestContext(request)) # I've also tried without context_instance, without passing errors...
else:
return render_to_response('visitorbrd.html')
网址.py
#urls.py
from django.conf.urls import patterns, include, url
from django.contrib import admin
admin.autodiscover()
from mission.views import *
urlpatterns = patterns('',
url(r'^admin/', include(admin.site.urls)),
url(r'^$', dashboard),
)
访客brd.html
{% extends "base.html" %}
{% block content %}
{% if state %}
<p>{{ state }}</p>
{% endif %}
<form action="." method="POST">{% csrf_token %}
<label for="username">User name:</label>
<input type="text" name="username" value="" id="username">
<label for="password">Password:</label>
<input type="password" name="password" value="" id="password">
<input type="submit" value="login" />
<input type="hidden" name="next" value="{{ next|escape }}" />
</form>
{% endblock %}
谢谢 !