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

时间:2017-02-16 19:06:59

标签: python django

我在Django中写了两个视图,以便为我的网站进行注册和登录。但问题是用户对象是否成功创建。但是当我稍后尝试进行身份验证时,会收到警告消息,表明具有该用户名的用户已经存在于Django中 下面给出了两种观点

Files.newInputStream(pathObject , StandardOpenOption.TRUNCATE_EXISTING);

这是我的forms.py' 来自django.contrib.auth.models导入用户 来自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,})

我该如何解决这个问题? THANKYOU

1 个答案:

答案 0 :(得分:3)

更改没有型号的表单的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)

这样,您的表单将验证是否输入了字段,并且不会从用户模型中获取验证