我正在尝试编写自己的 django 登录页面。一切都很好,直到我尝试登录,我只是被重定向回主登录视图。
模板
{% extends "bdayremind-maininput.html" %}
{% block main %}
<form class="form-horizontal" name="LoginForm" action="/login/" method="post">
{% csrf_token %}
{% if next %}
<input type="hidden" name="next" value="{{ next }}" />
{% endif %}
<div class="control-group">
<label class="control-label" for="username">Username</label>
<div class="controls">
<input type="text" id="username" value="{{username}}" placeholder="Username">
</div>
</div>
<div class="control-group">
<label class="control-label" for="password">Password</label>
<div class="controls">
<input type="password" name="password" id="password" placeholder="Password">
</div>
</div>
<div class="control-group">
<div class="controls">
<button type="submit" class="btn">Login</button>
</div>
</div>
</form>
{% endblock %}
网址
urlpatterns = patterns('',
url(r'^bdayremind_maininput/$', 'birthdayreminder.views.main'),
# login / logout
(r'^login/$', 'birthdayreminder.views.login_user'),
#(r'^logout/$', logout_page),
)
意见
import datetime
from django.http import *
from django.shortcuts import render_to_response
from django.template import RequestContext
from birthdayreminder.models import *
from django.contrib.auth.decorators import login_required
from django.contrib.auth import authenticate, login
def login_user(request):
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('birthdayreminder.views.main')
return render_to_response('login.html',{'username': username}, context_instance=RequestContext(request))
@login_required(login_url='/login/')
def main(request):
........
有任何想法吗 ?