Django无法使用login()登录

时间:2012-06-18 12:18:15

标签: python django authentication login

我正在尝试使用login()authenticate()创建登录页面,但是当我提交用户名和密码时,用户未经过身份验证(is_authenticated = False),同样好像我提交了错误的密码和/或用户名。

views.py:

from django.http import HttpResponseRedirect
from django.shortcuts import render_to_response
from django.template import RequestContext
from whitesnake.forms import RegistrationForm, LoginForm
from django.contrib.auth.models import User
from whitesnake.models import Whitesnake
from django.contrib.auth import authenticate, login, logout

def WhitesnakeLogin(request):
if request.user.is_authenticated():
    return render_to_response('index.html', None, context_instance = RequestContext(request))
if request.method =='POST':
    login_form = LoginForm(request.POST)
    if login_form.is_valid():
        username = login_form.cleaned_data['username']
        password = login_form.cleaned_data['password']
        whitesnake = authenticate(username = username, password = password)
        if whitesnake is not None:
            login(request, whitesnake)
            return HttpResponseRedirect('/admin/')
        else:
            login_form = LoginForm()
            context = {'login_form' : login_form}
            return render_to_response('index.html', context, context_instance = RequestContext(request))
    else:
        login_form = LoginForm()
        context = {'login_form' : login_form}
        return render_to_response('index.html', context, context_instance = RequestContext(request))        
else:
    login_form = LoginForm()
    context = {'login_form' : login_form}
    return render_to_response('index.html', context, context_instance = RequestContext(request))

forms.py:

from django import forms
from django.forms import ModelForm
from django.contrib.auth.models import User
from whitesnake.models import Whitesnake

class LoginForm(forms.Form):
    username = forms.EmailField(label=(u'Username'))
    password = forms.CharField(label=(u'Password'), widget = forms.PasswordInput(render_value = False))

我已经创建了一个注册页面,我确保我使用的用户名和密码是正确的并且已经存储在MySQL中。 Whitesnake是用户应用程序的名称

0 个答案:

没有答案