简体   繁体   English

具有该用户名的用户在Django中已经存在

[英]user with that username already exists in Django

I wrote two views as the class in Django in order to do the Registration and Login for my website. 我在Django中编写了两个视图作为类,以便为我的网站进行注册和登录。 But the problem is that the user objects get created successfully. 但是问题是用户对象创建成功。 But when I try to authenticate later getting the warning message showing that user with that username already exists in Django The two views are given below 但是当我稍后尝试进行身份验证时,收到警告消息,显示具有该用户名的用户已经存在于Django中。下面给出了两个视图

class RegistrationView(View):
    form_class=RegistrationForm
    template_name='eapp/user_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
            username =form.cleaned_data['username']
            password =form.cleaned_data['password']
            email=form.cleaned_data['email']

            user.set_password(password)
            user.save()

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


class LoginView(View):
    form_class=LoginForm
    template_name='eapp/user_login_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():


            #cleaned (normalized) data
            username =form.cleaned_data['username']
            password =form.cleaned_data['password']



            #authenticatin

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

            if user is not None:
                if user.is_active:
                        login(request,user)
                        return render(request,'eapp/index.html',{})

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

here is my forms.py' from django.contrib.auth.models import User from django import forms 这是我来自django.contrib.auth.models import的forms.py

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

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

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

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

] ]

How can I solve this? 我该如何解决? ThankYou 谢谢

Change your LoginForm for a Form without model: 将您的LoginForm更改为没有模型的表单:

from django import forms

class LoginForm(forms.Form):

    username = forms.CharField(label = 'Nombre de usuario')
    password = forms.CharField(label = 'Contraseña', widget = forms.PasswordInput)

This way your form will validate that the fields are entered and will not take validations from the User model 这样,您的表单将验证输入的字段,并且不会从用户模型中进行验证

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM