Django登录没有获得所有字段

时间:2016-03-06 17:21:29

标签: python django forms login field

我已经制作了一个自定义用户表单,它可用于注册和所有内容。但是当我尝试登录时,我的用户没有自定义表单中的任何属性。让我演示给你看: models.py

class RegisterUser(models.Model):
    user=models.OneToOneField(User,on_delete=models.CASCADE)

    biography = models.CharField(max_length=1000000,blank=True)
    research_place = models.CharField(max_length=1000000,blank=True)
    studies = models.CharField(max_length=1000000,blank=True)

forms.py

class RegistrationForm(UserCreationForm):

    fname = forms.CharField(max_length=256, label="", required=True, widget=forms.TextInput(attrs={'class': 'form-control','placeholder': 'First Name','required':True}))
    lname = forms.CharField(max_length=256, label="", required=True, widget=forms.TextInput(attrs={'class': 'form-control','placeholder': 'Last Name','required':True}))
    email = forms.EmailField(max_length=256, label="Email", required=True, widget=forms.EmailInput(attrs={'class': 'form-control','placeholder': 'Email','required':True, 'type':'email'}))
    emailConfirm = forms.EmailField(max_length=256, label="", required=True, widget=forms.EmailInput(attrs={'class': 'form-control','placeholder': 'Confirm Email','required':True, 'type':'email'}))
    password1 = forms.CharField(max_length=256, label="", required=True, widget=forms.PasswordInput(attrs={'class': 'form-control','placeholder': 'Password','required':True}))
    password2 =forms.CharField(required=False)

    biography = forms.CharField(label = "Biography",required=False)
    research_place = forms.CharField(label="Research Place",required=False)
    studies = forms.CharField(label="Studies",required=False)

    # class Meta(UserCreationForm.Meta): #extra bit of info
    #   model = user
    #   fields = ('email','emailConfirm','password1','biography','research_place','studies')
    class Meta(): #extra bit of info
        model = User
        fields = ('email','emailConfirm','password1','biography','research_place','studies')
    def clean_username(self):
        username = self.cleaned_data["username"]
        try:
            user.objects.get(username=username)
        except user.DoesNotExist:
            return username
        raise forms.ValidationError(self.error_messages['duplicate_username'])  

    def save(self,commit=True):
        user = super(RegistrationForm,self).save()
        user.first_name=self.cleaned_data['fname']
        user.last_name=self.cleaned_data['lname']
        # user.set_password(self.cleaned_data['password1'])
        user.username = self.cleaned_data['email']
        user.save()

        r_user = RegisterUser(user=user,biography="default1", research_place="default2", studies="default3")
        if commit:
            r_user.save()
            # print ('saving user: %s' % self.user)
        return r_user

最后是登录视图:

email=request.POST.get('email','')
password = request.POST.get('password','')
user = authenticate(username=email,password=password)

print(email)
print(password)
print(user)
if user is not None:
    print("notNone")
    login(request,user)
    return HttpResponseRedirect("out")

但如果我在模板中执行{{user.biography}},则不会显示任何内容。 has_attr还显示用户没有传记属性。有谁知道什么了?

1 个答案:

答案 0 :(得分:1)

biography字段位于RegisterUser模型,而不是User模型。因此,您必须{{ user.registeruser.biography }}而不是{{ user.biography }}