2016-03-25 40 views
0

編輯:如果你想跳過故事背景,只是檢查錯誤消息和代碼轉儲這一切都在底部:)「用戶」對象有沒有屬性「後臺」


我我一直試圖實施一個半天的用戶註冊表格,並覺得我很親密,但還沒有到那裏。

所以我開始實施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}) 

的問題是:我希望用戶能夠保存他們的電子郵件爲好。所以我決定用一段代碼從這個blogUserCreationForm()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 UserCreationFormfrom .forms import UserCreationForm)。最後,我在我的forms.py文件中將名稱切換到views.pyforms.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 

回答

3

docs

調用驗證()第一

當你手動,你必須成功 驗證與認證用戶登錄用戶()你面前調用login()。 authenticate()會在用戶身上設置一個屬性,指出哪個 認證後端成功認證了該用戶(有關詳細信息,請參閱 後端文檔),並且此後在登錄過程中需要此信息 。如果您嘗試登錄直接從數據庫中檢索到的用戶對象,將會引發錯誤。

因此,爲了登錄用戶,你必須首先調用authenticate(),並檢查是否返回用戶:

new_user = User.objects.create_user(username, email, password) 
new_user = authenticate(username=username, password=password) 
if new_user: 
    login(request, new_user)