我用MYUSER模型在Django 1.5與電子郵件登錄: 型號:登錄在Django 1.5
class MyUser(AbstractBaseUser):
email = models.EmailField(
verbose_name='email address',
max_length=255,
unique=True,
db_index=True,
)
last_name=models.CharField(max_length=30)
first_name=models.CharField(max_length=30)
second_name=models.CharField(max_length=30, blank=True)
post=models.CharField(max_length=30, blank=True)
objects = MyUserManager()
USERNAME_FIELD = 'email'
REQUIRED_FIELDS = ['last_name','first_name','second_name','post', ....]
def get_full_name(self):
return self.email
def get_short_name(self):
return self.email
def __unicode__(self):
return self.email
def has_perm(self, perm, obj=None):
return True
def has_module_perms(self, app_label):
return True
@property
def is_staff(self):
return self.is_admin
我試過方法與django.contrib.auth.views導入登錄:
網址:
(r'^login/$', login, {'template_name': 'enter.html'}),
查看:無。
它適用於經典用戶模型的超級用戶,而不適用於MyUser。
然後我嘗試:
觀點:
def login_view(request):
username = request.POST['email']
password = request.POST['password']
user = authenticate(username=username, password=password)
if user is not None and user.is_active:
login(request, user)
return HttpResponseRedirect("/n1.html")# Redirect to a success page.
else:
return HttpResponseRedirect("/account/invalid/")# Return a 'disabled account' error message
模板:
{% if form.errors %}
<p>Something is wrong</p>
{% endif %}
<form action="" method="post">
{% csrf_token %}
<label for="email">Login:</label>
<input type="text" name="email" value="" id="email"/>
<label for="password">Password:</label>
<input type="password" name="password" value="" id="username">
<input type="submit" value="login" />
<input type="hidden" name="next" value="{{next|escape}}" />
</form>
網址:
(r'^login/$', login_view, {'template_name': 'enter.html'}),
但我得到的錯誤login_view() got an unexpected keyword argument 'template_name'
我在做什麼錯了?
爲什麼你在'urls.py'中傳遞一個模板名稱,你在哪裏使用它? – arulmr 2013-02-22 07:19:57