【问题标题】:User has no profile in django form用户没有 django 形式的个人资料
【发布时间】:2016-11-03 07:33:57
【问题描述】:

我已经用个人资料表扩展了我的用户表,但是虽然用户网站的注册只询问用户类型“客户”和用户类型“促销员”的电子邮件和密码,但还有另一个注册表单需要其他字段,例如日期出生,地址,国家,州,城市等。但是登录后如果我们进入账户设置页面,两种类型的用户的账户设置页面是相同的,这就是为什么账户设置我创建了一个单一的django表单。

  class userAccountSettingsForm(forms.Form):
      email = forms.EmailField(required = True, max_length=50, widget=forms.EmailInput(attrs={
          'class' : 'custome-input promote-input', 
          'autocomplete' : 'off',
          'data-empty-message' : 'Please enter a email.',
          'data-error-message' : 'Please enter a valid email.',
          'readonly' : 'readonly'
      }))
      firstname = forms.CharField(required = True, max_length=50, widget=forms.TextInput(attrs={
          'class' : 'custome-input promote-input', 
          'autocomplete' : 'off',
          'data-empty-message':'Please enter first name.' 
      }))
      lastname = forms.CharField(required = True, max_length=50, widget=forms.TextInput(attrs={
          'class' : 'custome-input promote-input', 
          'autocomplete' : 'off',
          'data-empty-message':'Please enter last name.' 
      }))
      dob = forms.DateField(required = False, widget=forms.DateInput(attrs={
          'class' : 'custome-input promote-input',
          'id' : 'custom-datepicker',
          'autocomplete' : 'off',
          'readonly':'readonly'
      }))
      phone = forms.IntegerField(required = True, widget=forms.TextInput(attrs={
          'class' : 'custome-input promote-input',
          'autocomplete' : 'off',
          'data-empty-message':'Please enter mobile number.'
      }))
      address = forms.CharField(required = False, max_length=225, widget=forms.TextInput(attrs={
          'class' : 'custome-input promote-input',
          'autocomplete' : 'off'
      }))
      subscription = forms.BooleanField(required = False)

问题是,在注册发起人时,它还会在配置文件表中创建它的行,而在注册客户时,它只在用户表中创建行,所以这就是为什么在帐户设置页面中我必须从模型中找出用户的数据,我是在视图的 django 表单的帮助下执行此操作,这是我的视图代码:-

dobForm=""
if my_details.profile.dob:
    dobForm = datetime.strptime(str(my_details.profile.dob), '%Y-%m-%d').strftime('%m/%d/%Y')
settingsForm = userAccountSettingsForm(
    initial={
        'email': my_details.email,
        'firstname': my_details.first_name,
        'lastname': my_details.last_name,
        'dob': dobForm,
        'phone':my_details.profile.phone_number,
        'address':my_details.profile.address,
        'subscription':my_details.profile.subscription,
        }
    )

因此,使用此页面它会获取用户的所有现有数据,如果是用户类型的推广者,它不会产生任何问题,因为注册时配置文件表中已经为推广者生成了一行,但如果是客户,它不会生成行注册时在个人资料表中的客户,在这种情况下它会给出错误:-

RelatedObjectDoesNotExist at /home/account-settings

    User has no profile

除了在客户注册时在个人资料表中创建一行之外,还有其他解决方案吗?

【问题讨论】:

  • 有什么解决办法吗?
  • 尝试将此if my_details.profile.dob: 更改为hasattr(my_details, 'profile')

标签: python django


【解决方案1】:

如果对象不存在,OneToOneField 将引发此异常。所以你只需要捕获那个异常。

其他解决方案可能是使用 hasattr,正如 Sagar 在 cmets 中所建议的那样,但如果您使用 python issues。

【讨论】:

    猜你喜欢
    • 2020-05-20
    • 1970-01-01
    • 1970-01-01
    • 2023-03-22
    • 1970-01-01
    • 2021-09-04
    • 2020-12-14
    • 1970-01-01
    • 2021-10-13
    相关资源
    最近更新 更多