我写了一个用户登录功能,但它不起作用
这是models.py
class UserProfile(models.Model):
Userinfo = models.OneToOneField(User)
join_time = models.DateTimeField()
def __unicode__(self):
return self.Userinfo.username
这是views.py
中的userlogindef Userlogin(request):
uf = UserForm(request.POST)
if request.method == "POST":
if uf.is_valid():
uf = UserForm(request.POST)
username = uf.cleaned_data['username']
password = uf.cleaned_data['password']
user = models.UserProfile.authenticate(
username=username,password=password)
if user is not None:
if user.is_active:
login(request, user)
return render(request,
'UserLogin/index.html',{'username':username})
else:
return HttpResponse('password is not matched')
return render(request, 'UserLogin/login.html',{'uf':uf})
我多次阅读django的文件但我觉得我并不是真的明白。 那么我使用用户模型的方式是否正确?这真让人困惑......
答案 0 :(得分:1)
原因是您在验证后重新分配表单的值。如果是is_valid()
,则可以访问cleaned data
,但之前不能访问def Userlogin(request):
uf = UserForm(request.POST)
if request.method == "POST":
if uf.is_valid():
#uf = UserForm(request.POST)
username = uf.cleaned_data['username']
password = uf.cleaned_data['password']
。尝试更改你刚评论的recond uf assignation:
TableLayout