现在很多系统的登录模块,已经都使用email来代替传统的username(为什么用这么做?我想是便于系统能给每个用户发送“垃圾邮件“吧,哈哈)
但是最新版的django(目前使用的是1.6.1)仍然只支持使用username/password组合实现登录:
from django.contrib.auth import authenticate, login
def my_view(request):
username = request.POST['username']
password = request.POST['password']
user = authenticate(username=username, password=password)
if user is not None:
if user.is_active:
login(request, user)
# 跳转到成功页面.
else:
# 返回一个无效帐户的错误
else:
# 返回登录失败页面。
为了能使用email,网上找了些solution,先记录一下。
https://github.com/dabapps/django-email-as-username
http://justcramer.com/2008/08/23/logging-in-with-email-addresses-in-django/