[英]Django 1.9 error - 'User' object has no attribute 'profile'
So I recently added an optional user profile model that is linked to a user via a OneToOneField like so:所以我最近添加了一个可选的用户配置文件模型,它通过 OneToOneField 链接到用户,如下所示:
class UserProfile(models.Model): # Creating class
user = models.OneToOneField(User, on_delete=models.CASCADE)
This worked fine, and my current UserProfile models were intact both before and after I added in this field to link the profile to a user.这工作正常,并且我当前的 UserProfile 模型在我添加此字段以将配置文件链接到用户之前和之后都完好无损。
The problem comes when I log in on the website as a valid user, after submitting the log in form comes an error:当我以有效用户身份登录网站时出现问题,提交登录表单后出现错误:
AttributeError at /login/
/login/ 处的属性错误
'User' object has no attribute 'profile'
“用户”对象没有“个人资料”属性
I have searched through my files for anything along the lines of ' User.profile
', and similar queries, however I can't seem to find anything that would allow me to properly log in as a user.我已经在我的文件中搜索了“
User.profile
”和类似查询中的任何内容,但是我似乎找不到任何可以让我以用户身份正确登录的内容。
I wasn't sure what other code to post here, and didn't want to spam this question with potentially unnecessary code so tell me if there's anything more specific (views.py, models.py, forms.py, etc) that will be needed to solve this sort of problem, or if you know of any possible solutions.我不确定要在此处发布哪些其他代码,并且不想用可能不必要的代码向此问题发送垃圾邮件,因此请告诉我是否有更具体的内容(views.py、models.py、forms.py 等)需要解决此类问题,或者如果您知道任何可能的解决方案。
Thank you谢谢
您尚未在该一对一字段上设置任何 related_name 属性,因此反向访问器将被称为userprofile
而不是profile
。
确保您的应用在 settins.py INSTALLED_APPS 中
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.