2017-02-16 199 views
0

我在Django中編寫了兩個視圖作爲類,以便爲我的網站進行註冊和登錄。但問題是用戶對象已成功創建。但是,當我嘗試以後得到驗證顯示該用戶的警告信息與該用戶名在Django 已經存在兩種觀點給出如下該用戶名已經存在於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,}) 

這裏是我的forms.py」 從django.contrib.auth。從Django的進口車型的形式導入用戶

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' 

]

我怎樣才能解決這個問題?三江源

+0

需要驗證你可以分享你的形式的.py? –

+0

@BrianOcampo一秒鐘兄弟 – Ananthu

+0

@BrianOcampo更新 – Ananthu

回答

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) 

這樣,你的形式將驗證字段中輸入並不會從用戶模型

相關問題