'用户'对象没有属性'后端'

时间:2016-03-25 15:57:32

标签: django forms

编辑:如果您想跳过背景故事,只需查看错误消息并将代码转储到底部:)

我一直试图用半天的时间来实现一个用户注册表单,觉得我已经很接近了但还没有完成。

所以我开始实现内置UserCreationForm的Django。而对于我的观点,我根据对此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.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'

我谷歌它,要么我找不到任何与我的情况相关的东西,要么我迟迟不知道它会发生什么。如果那里有人可以解释什么是错的,我会非常感激。

当前代码的代码转储:
为了便于阅读,省略了其他观点和不相关的导入

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})

forms.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

2 个答案:

答案 0 :(得分:3)

来自docs

  

首先调用authenticate()

     

当您手动登录用户时,您必须成功   在调用login()之前使用authenticate()对用户进行身份验证。   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)

答案 1 :(得分:0)

在调用login()之前,必须在用户上调用authenticate()。这将检查是否返回了用户,并且不允许您直接从数据库中调用该用户。该代码段应适用于您清除的数据。

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)
            new_user = authenticate(**form.cleaned_data)
            login(request, new_user)
            return redirect('post_list')
    else:
        form = UserCreationForm()

    return render(request, 'blog/create_user.html', {'form': form})