0
我想創建一個註冊視圖。在這裏,我的代碼:Django註冊視圖
from django.shortcuts import get_object_or_404, render, redirect
from django.contrib.auth import authenticate, login as auth_login
from django.contrib.auth.models import User
# create a function to resolve email to username
def get_user(email):
try:
return User.objects.get(email=email.lower())
except User.DoesNotExist:
return None
def signup(request):
if request.method == "GET":
return render(request, 'accounts/signup.html')
if request.method == "POST":
email = request.POST['email']
last_name = request.POST['last_name']
first_name = request.POST['first_name']
password = request.POST['password']
password2 = request.POST['password2']
user = get_user(email)
if password == password2:
if user is None:
user = User.objects.create_user(last_name, email, password)
user.first_name = first_name
user.save()
user = authenticate(username=user, password=password)
#login
return redirect('/')
else:
#messages
return render(request, 'accounts/signup.html')
else:
#messages
return render(request, 'accounts/signup.html')
我需要你做得更好,因爲如你所見,他有點混亂。
我不知道是否最好使用像django註冊這樣的插件來完成這個任務?
非常感謝!
它看起來很不錯。您可以使用基於類的視圖:https://docs.djangoproject.com/en/dev/topics/class-based-views/。 –
爲什麼不使用表單?它會讓你的生活變得如此簡單。 Django註冊,IMO可能是一個不錯的選擇,因爲它會減少你必須管理的大量代碼 – karthikr
這可能是有用的:http://justcramer.com/2008/08/23/logging-in-with -email-地址合的django / –