0
我試圖使用與Django的nonrel用戶授權的自定義後端(電子郵件adrresses而不是用戶名,沒有什麼花哨),它只是不工作。所有致電authenticate()
的電話均返回無。這裏是我的後端:我可以使用django-nonrel和自定義auth後端嗎?
from django.contrib.auth.models import User, check_password
class EmailAuthBackend(object):
"""
Email Authentication Backend
Allows a user to sign in using an email/password pair rather than
a username/password pair.
"""
supports_anonymous_user = False
supports_object_permissions = True
def authenticate(self, email=None, password=None):
""" Authenticate a user based on email address as the user name. """
try:
user = User.objects.get(email=email)
if user.check_password(password):
return user
except User.DoesNotExist:
return None
def get_user(self, user_id):
""" Get a User object from the user_id. """
try:
return User.objects.get(pk=user_id)
except User.DoesNotExist:
return None
和視圖(我試圖在註冊後立即登錄用戶)。 new_user
總是無:
def create_new_user(request):
if request.method == 'POST':
data = request.POST.copy()
data['username'] = ''.join([choice(letters) for i in xrange(30)])
data['password'] = data['password1']
form = CustomUserCreationForm(data)
if form.is_valid():
user = form.save(commit=False)
# user must be active for login to work
user.is_active = True
user.save() #not sure if this is actually saving
new_user = authenticate(username=data['username'],\
password=data['password'])
if new_user is None:
messages.error(request,"God damn it.")
login(request, new_user)
messages.info(request, "Thanks for registering! You are\
now logged in.")
return render_to_response('invoicer/welcome.html',
{'user':new_user})
else:
messages.error(request, form._errors)
return HttpResponseRedirect('//')
誰能給我一些啓發,爲什麼我到authenticate()
調用不工作?
編輯:另外,在部署時,我的註冊表格肯定是創建用戶可在數據查看器中看到appengine.google.com