Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/287.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 属性错误at/profile/_Python_Django_Django Models_Django Views - Fatal编程技术网

Python 属性错误at/profile/

Python 属性错误at/profile/,python,django,django-models,django-views,Python,Django,Django Models,Django Views,“用户”对象没有属性“配置文件” class Profile(models.Model): user = models.OneToOneField(User, on_delete=models.CASCADE) 当我单击profile(配置文件)按钮查看用户的配置文件时,问题出现了。请尝试使用此配置文件并再次注册:- from django.db.models.signals import post_save from django.contrib.auth.models impor

“用户”对象没有属性“配置文件”

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

当我单击profile(配置文件)按钮查看用户的配置文件时,问题出现了。请尝试使用此配置文件并再次注册:-


from django.db.models.signals import post_save
from django.contrib.auth.models import User
from django.dispatch import receiver

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

    @receiver(post_save,sender=User)
    def post_save_create_profile(sender,instance,created,**kwargs):
        if created:
            Profile.objects.create(user=instance)
我改变了什么:-

我添加了post_save_create_profile函数,以便在注册用户时添加
profile
。它将保存用户并为用户保存一个
配置文件

注意:不要忘记导入(接收者,后期保存),并在
模型.py中的更改后立即运行
python manage.py makemigrations
python manage.py migrate


更改模型后是否运行了
python manage.py makemigrations
python manage.py migrate
?是否可以发布实际回溯和完整模型?AttributeError at/profile/'User'对象没有属性'profile'请求方法:获取请求URL:Django版本:2.0.7异常类型:AttributeError异常值:“用户”对象没有属性“配置文件”