Django - authenticate()具有该用户名的用户已存在

时间:2017-03-14 17:30:26

标签: python django authentication login

好吧,我很困惑。

我正在尝试构建登录页面,但每当我尝试登录时,django都会提供用户名已存在的错误。我还没有在任何地方使用save()

我使用authenticate(),我提到了Django文档: https://docs.djangoproject.com/en/1.10/topics/auth/default/#how-to-log-a-user-in

这是我的代码,请告诉我哪里出错:

forms.py

class LoginForm(forms.ModelForm):
    username = forms.CharField(widget=forms.TextInput(attrs={'placeholder': 'Username'}))
    password = forms.CharField(widget=forms.PasswordInput(attrs={'placeholder': 'Password'}))

class Meta:
    model = User
    fields = ['username', 'password']

views.py

class LoginFormView(View):
    form_class = LoginForm
    template_name = 'login.html'

    # display a blank form
    def get(self, request):
        form = self.form_class(None)
        return render(request, self.template_name, {'form': form})

    # authenticate user
    def post(self, request):
        form = self.form_class(request.POST)

        if form.is_valid():

            username = form.cleaned_data['username']
            password = form.cleaned_data['password']

            user = authenticate(username=username, password=password)

            if user is not None:
                if user.is_active:
                    login(request, user)
                    return redirect('slrtcebook:home')

        return render(request, self.template_name, {'form': form})

的login.html

<div class="form-container">
    <form method="post" enctype="multipart/form-data">

        {% csrf_token %}
        {% for field in form %}
            {{ field }}
            {{ field.errors }}
        {% endfor %}

        <input id="submit" type="submit" value="Log in" />
    </form>
</div>

<p>Don't have an account? <a href="/">Register here</a></p>

2 个答案:

答案 0 :(得分:12)

不要使用ModelForm;它将假设您尝试创建用户,并验证您是否可以使用您输入的数据执行此操作。只需使用标准表单 - 从forms.Form继承并删除Meta类。

答案 1 :(得分:0)

对于那些想要在此处编写代码的人来说,我是在修改它的:

views.py内部:

class UserLoginView(View):
form_class = LoginForm
template_name = 'music/login_form.html'

#display a blank form
def get(self, request):
    form = self.form_class(None)
    return render(request, self.template_name, {'form': form})

#proces form data
def post(self, request):
    form = self.form_class(request.POST)

    if form.is_valid():

        # user = form.save(commit=False)

        #cleaned (normalized) data
        username = form.cleaned_data['username']
        password = form.cleaned_data['password']
        # user.set_password(password) #this is the only way to change a password because of hashing

        #returns the User obejects if credintials are correct
        user = authenticate(username=username, password=password)

        if user is not None:
            if user.is_active:
                login(request, user)
                return redirect('music:index')


    return render(request, self.template_name,{'form': form})

在froms.py内部:

class LoginForm(forms.Form):
username = forms.CharField(widget=forms.TextInput(attrs={'placeholder': 'Username'}))
password = forms.CharField(widget=forms.PasswordInput(attrs={'placeholder': 'Password'}))
fields = ['username', 'password']

别忘了还要在views.py顶部导入LoginForm,在其中导入UserForm:

from .forms import UserForm, LoginForm