如何在已经buit项目中将用户名验证更改为电子邮件验证?

时间:2017-06-16 20:00:05

标签: python django authentication django-forms django-rest-framework

我最初习惯使用用户名和密码登录,但现在想要将其更改为同一站点中的电子邮件地址身份验证,而无需删除现有用户和内容。 我在我的文件中进行了以下更改但得到错误'UNIQUE约束失败:auth_user.username'

forms.py

from django.contrib.auth.models import User
from django import forms


class UserForm(forms.ModelForm):
    password = forms.CharField(widget=forms.PasswordInput)

    class Meta:
        model = User
        fields = ['first_name', 'last_name', 'email', 'password']

views.py

class UserFormView(View):

    form_class = UserForm
    template_name = 'main/registration_form.html'

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

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

        if form.is_valid():

            user = form.save(commit=False)

            # cleaned (normalized) data
            email = form.cleaned_data['email']
            password = form.cleaned_data['password']
            user.set_password(password)
            user.save()

            # return user objects if credentials are correct
            user = authenticate(email=email, password=password)

            if user is not None:

                if user.is_active:
                    login(request, user)
                    # request.user.username       display their username
                    return redirect('main:register2')
        return render(request, self.template_name, {'form': form})

class LoginForm(View):

    form_class = UserForm
    template_name = 'main/login_form.html'

    def get(self, request):
        form = self.form_class(None)
        if error:
            return render(request, self.template_name, {'form': form}, 
{'error': error})
        else:
            return render(request, self.template_name, {'form': form})

    def post(self, request):
        email = request.POST.get('email', '')
        password = request.POST.get('password', '')
        user = auth.authenticate(email=email, password=password)

        if user is not None and user.is_active:
            # Correct password, and the user is marked "active"
            auth.login(request, user)
            # Redirect to a success page.
            return HttpResponseRedirect("/main/home/")
        else:
            # Show an error page
            error = True
            return HttpResponseRedirect("/main/", {'error': error})

我得到的错误 - http://dpaste.com/0ZVHW0P

请回答考虑到我是django新手的事实。 提前感谢你:))

1 个答案:

答案 0 :(得分:0)

因此,即使您想使用电子邮件地址作为标识符,如果您想使用Django的auth框架,也必须使用auth表中的username字段。方框'。

上面发生的是您没有设置用户名,这意味着您的auth_user表中有空用户名。由于用户名必须是唯一的,因此当您尝试添加没有用户名的第二个用户时,您将获得数据库级完整性约束UNIQUE constraint failed: auth_user.username

要解决此问题,请将views.py更改为如下所示:

class UserFormView(View):

    form_class = UserForm
    template_name = 'main/registration_form.html'

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

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

        if form.is_valid():

            user = form.save(commit=False)

            # cleaned (normalized) data
            email = form.cleaned_data['email']
            password = form.cleaned_data['password']
            user.username = email
            user.set_password(password)
            user.save()

            # return user objects if credentials are correct
            user = authenticate(username=email, password=password)

            if user is not None:

                if user.is_active:
                    login(request, user)
                    # request.user.username       display their username
                    return redirect('main:register2')
        return render(request, self.template_name, {'form': form})

class LoginForm(View):

    form_class = UserForm
    template_name = 'main/login_form.html'