python - Django 登录未获取所有字段

标签 python django forms authentication field

我制作了一个自定义用户表单,它可以用于注册和所有操作。但是当我尝试登录时,我的用户没有自定义表单中的任何属性。我来给你展示: 模型.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)

表单.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

最后是登录 View :

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 还显示用户没有传记属性。有谁知道怎么回事吗?

最佳答案

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

关于python - Django 登录未获取所有字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35829962/

相关文章:

python - 运行 '$x("XPath")' in Selenium ' driver.execute_script' JavaScript, chromedriver=111.0.5563.64

django - 根据 django 项目中的用户更改时区

javascript - PHP 使用 post 以编程方式导航到新表单

forms - ionic 3 形式仍然警告我 : "Password field is not contained in a form"

php - 访问 $_POST 时出现未定义索引错误

python - 为什么 Keras 不返回 lstm 层中细胞状态的完整序列?

python - 为什么 os.popen 的返回对象不支持 next() 调用

python - 为什么 AWS SageMaker 运行 Web 服务器进行批量转换?

python - 注销后Django重新加载并重新登录

django - 如何修改 Django 管理过滤器的标题