Django身份验证(自定义登录页面)

时间:2013-05-07 20:51:41

标签: django

我正在尝试编写自己的django登录页面。 Eveything可以直到我尝试登录,我只是被重定向回主登录视图。

TEMPLATE

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

URLS

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):
........

任何想法?

1 个答案:

答案 0 :(得分:1)

您忘记在登录后添加重定向到所需的页面。