[英]Django auth/login problems
我是Django的新手,並嘗試使用django.contrib.auth.views.login
和Django 1.4為我的網頁構建一個簡單的登錄系統。 我有一個包含以下登錄表單的基本模板,然后通過我網站上的其他模板頁面進行了擴展:
<form method="post" action="/accounts/login/">
{% csrf_token %}
<p><label for="id_username">Username:</label> <input id="id_username" type="text" name="username" maxlength="30" /></p>
<p><label for="id_password">Password:</label> <input type="password" name="password" id="id_password" /></p>
<input type="submit" value="Log in" />
<input type="hidden" name="next" value="{{ request.get_full_path }}" />
</form>
但是,當我嘗試登錄時,出現以下消息:
“禁止(403)CSRF驗證失敗。請求中止。失敗的原因:CSRF令牌丟失或不正確。”
urls.py中的相關片段:
url(r'^accounts/login/$', 'django.contrib.auth.views.login')
和settings.py:
TEMPLATE_CONTEXT_PROCESSORS = (
'django.core.context_processors.request',
)
.....
INSTALLED_APPS = (
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
'django.contrib.messages',
'django.contrib.staticfiles',
)
.....
MIDDLEWARE_CLASSES = (
'django.middleware.common.CommonMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
)
有什么建議么?
讓我簡要說明一下如何通過Django中的用戶模型使用標准身份驗證 :
appname / views.py:
from django.http import HttpResponse
from django.contrib.auth import authenticate, login
from django.contrib.auth.decorators import login_required
from django.template import Context, loader, RequestContext
from django.shortcuts import render_to_response
from django.template import
@login_required
def stat_info(request):
return render_to_response('stat_info.html',
{'is_auth':request.user.is_authenticated()},
context_instance=RequestContext(request))
@login_required
def mainmenu(request):
return render_to_response('mainmenu.html',{},
context_instance=RequestContext(request))
urls.py:
from django.conf.urls import patterns, include, url
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
url(r'^admin/', include(admin.site.urls)),
(r'^statinfo/$', 'appname.views.stat_info'),
(r'^accounts/login/$', 'django.contrib.auth.views.login'),
(r'^accounts/logout/$', 'django.contrib.auth.views.logout', {'next_page' : '/accounts/login'}),
(r'^mainmenu/$', 'appname.views.mainmenu')
)
settings.py:
...
LOGIN_REDIRECT_URL='/mainmenu/'
...
模板/注冊/login.html:
{% extends "base.html" %}
{% block content %}
{% if form.errors %}
<p>Your username and password didn't match. Please try again.</p>
{% endif %}
<form method="post" action="{% url django.contrib.auth.views.login %}">
{% csrf_token %}
<table>
<tr>
<td>{{ form.username.label_tag }}</td>
<td>{{ form.username }}</td>
</tr>
<tr>
<td>{{ form.password.label_tag }}</td>
<td>{{ form.password }}</td>
</tr>
</table>
<input type="submit" value="login" />
<input type="hidden" name="next" value="{{ next }}" />
</form>
{% endblock %}
templates / base.html:
<!DOCTYPE html>
<html lang="en">
<head>
<link rel="stylesheet" href="style.css" />
<title>{% block title %}templates/base.html{% endblock %}</title>
</head>
<body>
<div id="sidebar">
{% block sidebar %}
<ul>
<li><a href="/">Home</a></li>
{% if user.is_authenticated %}
<li><a href="/accounts/logout">Logout</a></li>
{% else %}
<li><a href="/accounts/login">Login</a></li>
{% endif %}
</ul>
{% endblock %}
</div>
<div id="content">
{% block content %}{% endblock %}
</div>
</body>
</html>
templates / mainmenu.html:
<!DOCTYPE html>
{% extends "base.html" %}
<html>
<head>
<title>{% block title %}templates/mainmenu.html{% endblock %}</title>
</head>
<body>
<div id="content">
{% block content %}
Mainmenu
<a href="/statinfo/">stat info</a>
{% endblock %}
</div>
</body>
</html>
template / stat_info.html:
<!DOCTYPE html>
{% extends "base.html" %}
<html>
<head>
<title>{% block title %}templates/mainmenu.html{% endblock %}</title>
</head>
<body>
<div id="content">
{% block content %}
Mainmenu
<a href="/statinfo/">stat info</a>
{% endblock %}
</div>
</body>
</html>
楷模:
類UserProf(models.Model):
"""
Create ``RegistrationProfile for User``
"""
user = models.OneToOneField(User)
""" Other fields here """
address1 = models.CharField(max_length=100,blank=True)
address2 = models.CharField(max_length=100,blank=True)
city = models.CharField(max_length=40,blank=True)
country = models.CharField(max_length=40,blank=True)
age = models.IntegerField(blank=True)
意見:
從django.contrib.auth導入身份驗證,登錄
def user_authenticate(請求):
"""
check username and password and displays its appr. user_profile page
"""
username = password = ''
if request.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('/profile/')
else:
return HttpResponseRedirect('/qioness/connect/')
else:
return HttpResponseRedirect('/qioness/connect/')
網址:
url(r'^ profile / $',show_detail),
模板:
<form action="/qioness/" method="post">
<label>
<strong class="email-label">Username</strong>
<input type="text" id="Email" name="username">
<span class="login_error"></span>
</label>
<label>
<strong class="passwd-label">Password</strong>
<input type="password" id="Passwd" name="Password">
<span class="login_error"> </span>
</label>
<input name="Add" type="submit" value="Log" class="Submit_btn" />
<label class="remember">
<input type="checkbox" value="yes" id="PersistentCookie" name="PersistentCookie">
<strong class="remember-label">
Remember Me
</strong>
</label>
</form>
</div>
工作了4我:)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.