編輯:如果你想跳過故事背景,只是檢查錯誤消息和代碼轉儲這一切都在底部:)「用戶」對象有沒有屬性「後臺」
我我一直試圖實施一個半天的用戶註冊表格,並覺得我很親密,但還沒有到那裏。
所以我開始實施Django的內置UserCreationForm
。而對於我的看法我cunstructed這段代碼通過這個question響應啓發:
from django.contrib.auth.forms import UserCreationForm
from django.contrib.auth import login
def create_user(request):
if request.method == "POST":
form = UserCreationForm(request.POST)
if form.is_valid():
new_user = User.objects.create_user(**form.cleaned_data)
login(new_user)
return redirect('post_list')
else:
form = UserCreationForm()
return render(request, 'blog/create_user.html', {'form': form})
的問題是:我希望用戶能夠保存他們的電子郵件爲好。所以我決定用一段代碼從這個blog到UserCreationForm()
在forms.py
文件擴展:
from django import forms
from django.contrib.auth.models import User
from django.contrib.auth.forms import UserCreationForm
class UserCreateForm(UserCreationForm):
email = forms.EmailField(required=True)
class Meta:
model = User
fields = ("username", "email", "password1", "password2")
def save(self, commit=True):
user = super(UserCreateForm, self).save(commit=False)
user.email = self.cleaned_data["email"]
if commit:
user.save()
return user
它仍然拒絕將電子郵件的一部分,當我檢查了實際的網頁(即使我切換出:from django.contrib.auth.forms import UserCreationForm
至from .forms import UserCreationForm
)。最後,我在我的forms.py文件中將名稱切換到views.py
和forms.py
文件中的UserForm。所以在forms.py
文件,現在說:
class UserForm(UserCreationForm):
解決這個question啓發。
所以現在說它'password1' is an invalid keyword argument for this function
,我想「當然,我將字典從我的表單傳遞到create_user,現在我已經篡改了UserCreationForm
它不再流暢運行:create_user希望擁有?一個用戶名,電子郵件地址和密碼對的,但現在我發送用戶名,電子郵件,密碼1及密碼2
所以我加了下面的代碼在views.py
:
if form.is_valid():
username = form.cleaned_data["username"]
email = form.cleaned_data["email"]
password = form.cleaned_data["password1"]
new_user = User.objects.create_user(username, email, password)
login(new_user)
所以現在我得到TypeError at /create_user/: login() takes exactly 2 arguments (1 given)
和真正研究我的齒輪的是,在我被篡改之前,這不是一個問題UserCreationForm
(據我的回憶)。但好吧,好吧,我會谷歌,通過文檔和一些例子看,貌似大多數人打電話與login(request, user)
登錄功能,所以我改變我的代碼再次:
new_user = User.objects.create_user(username, email, password)
login(request, new_user)
在這裏,我米終於陷入了僵局,因爲我得到:
'User' object has no attribute 'backend'
我google一下,要麼我沒有找到任何與我的情況相關,或者我慢機智地看到它。如果有誰可以解釋發生了什麼問題,我將不勝感激。當前代碼
代碼轉儲:
省略了其它意見和可讀性
views.py
from django.shortcuts import render
from .forms import UserForm
from django.shortcuts import redirect
from django.contrib.auth.models import User
from django.contrib.auth import login
def create_user(request):
if request.method == "POST":
form = UserForm(request.POST)
if form.is_valid():
username = form.cleaned_data["username"]
email = form.cleaned_data["email"]
password = form.cleaned_data["password1"]
new_user = User.objects.create_user(username, email, password)
login(new_user)
return redirect('post_list')
else:
form = UserForm()
return render(request, 'blog/create_user.html', {'form': form})
形式。PY
from django import forms
from django.contrib.auth.models import User
from django.contrib.auth.forms import UserCreationForm
class UserForm(UserCreationForm):
email = forms.EmailField(required=True)
class Meta:
model = User
fields = ("username", "email", "password1", "password2")
def save(self, commit=True):
user = super(UserForm, self).save(commit=False)
user.email = self.cleaned_data["email"]
if commit:
user.save()
return user