我从 djangoUser
类中继承,以创建我自己的个性化版本。User
我可以谈论的课程是django.contrib.auth.models.User
。
这是它的样子:
from django.db import models
from django.contrib.auth.models import User as Auth_User
class User(Auth_User):
# User data
Age = models.IntegerField(blank=False, default=0)
middle_name = models.CharField(blank=True, max_length=255)
# Properties
@property
def full_name(self):
return str(self.first_name + ' ' + (self.middle_name + ' ' if self.middle_name else '') + self.last_name)
# Overridden functions
def __unicode__(self):
return self.full_name
请不要说这是模型的简化版本,有更多数据,与这个问题无关。
现在,当我尝试通过传统方式进行身份验证时,它失败了:
def login(request):
if request.method == 'GET':
return render(request, "User/login.html")
if request.method == 'POST':
username = request.POST['username']
password = request.POST['password']
new_user = authenticate(username=username, password=password)
if new_user is not None:
if new_user.is_active:
login(request, new_user)
else:
print "User is not alive, he/she died! :("
return HttpResponseRedirect('/users/')
HTML 没什么特别的:
{% extends 'layout.html' %}
{% block content %}
<form action="/users/login" method="post">
{% csrf_token %}
<label for="username">
Username:
<input type="text" name="username" id="username"/>
</label>
<label for="password">
Password:
<input type="password" name="password" id="password"/>
</label>
<input type="submit" value="Submit"/>
</form>
{% endblock %}
那么,如何验证User
从 django 用户类继承的我的类?