Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/22.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 MyProfile匹配查询不存在_Python_Django - Fatal编程技术网

Python MyProfile匹配查询不存在

Python MyProfile匹配查询不存在,python,django,Python,Django,事情就是这样发生的; DoesNotExist at/post/naver/ 我在这一行的末尾添加了id,profile=MyProfile.objects.get(user=request.user.id),因此出现了这个错误 我正在使用userena。我必须使用id,否则它不会给出“SimpleLazyObject” 这是回溯 Traceback: /env/local/lib/python2.7/site-packages/django/core/handlers/base.py" in

事情就是这样发生的; DoesNotExist at/post/naver/

我在这一行的末尾添加了id,
profile=MyProfile.objects.get(user=request.user.id),因此出现了这个错误
我正在使用userena。我必须使用id,否则它不会给出“SimpleLazyObject”

这是回溯

Traceback:
/env/local/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response
  132.                     response = wrapped_callback(request, *callback_args, **callback_kwargs)
/ebagu/main/views.py" in post
  87.       profile = MyProfile.objects.get(user=request.user.id)
/env/local/lib/python2.7/site-packages/django/db/models/manager.py" in manager_method
  127.                 return getattr(self.get_queryset(), name)(*args, **kwargs)
/env/local/lib/python2.7/site-packages/django/db/models/query.py" in get
  334.                 self.model._meta.object_name

Exception Type: DoesNotExist at /post/naver/
Exception Value: MyProfile matching query does not exist.
我认为你应该使用

profile = MyProfile.objects.get(user_id=request.user.id)
我认为你应该使用

profile = MyProfile.objects.get(user_id=request.user.id)

根据其他问题,我假设
MyProfile
是用户的一对一字段。所以你可以通过

 request.user.myprofile

根据其他问题,我假设
MyProfile
是用户的一对一字段。所以你可以通过

 request.user.myprofile

确保模型(MyProfile)中存在用户


确保模型(MyProfile)中存在用户


如果不能保证配置文件存在于数据库中,则需要编写代码来处理这种情况。一种常见的方法是捕获
DoesNotExist
异常

try:
    profile = MyProfile.objects.get(user=request.user)
    # if it's a OneToOne field, you can do:
    # profile = request.user.myprofile
except MyProfile.DoesNotExist:
    profile = None
    # other code that handles missing profile
如果所有登录的用户都有配置文件,但您的视图也在处理匿名用户,那么如果改用If语句,代码可能会更清晰。可以实例化为匿名用户显示默认值的配置文件

if request.user.is_authenticated():
    profile = request.user.myprofile
else:
    profile = MyProfile(favourite_food="pasta", favourite_colour="blue")

如果不能保证配置文件存在于数据库中,则需要编写代码来处理这种情况。一种常见的方法是捕获
DoesNotExist
异常

try:
    profile = MyProfile.objects.get(user=request.user)
    # if it's a OneToOne field, you can do:
    # profile = request.user.myprofile
except MyProfile.DoesNotExist:
    profile = None
    # other code that handles missing profile
如果所有登录的用户都有配置文件,但您的视图也在处理匿名用户,那么如果改用If语句,代码可能会更清晰。可以实例化为匿名用户显示默认值的配置文件

if request.user.is_authenticated():
    profile = request.user.myprofile
else:
    profile = MyProfile(favourite_food="pasta", favourite_colour="blue")


或者只是
user=request.user
你确定
MyProfile
对象存在吗?或者只是
user=request.user
你确定
MyProfile
对象存在吗?一个更好的选择可能是
try:
get()
除了MyProfile.DoesNotExist:
,这避免了对
exists()的额外查询
@Alasdair您能用try和except?向我展示解决方案吗?我尝试过,但不起作用…使用此解决方案,我输入request.user.id,而不仅仅是request.user,我在分配前引用了局部变量'profile'。我已经使用try.添加了一个答案。下面除外。有没有办法为未登录的用户显示配置文件?我使用这个ppl谁没有登录。用户一个更好的选择可能是
try:
get()
,除了MyProfile之外。DoesNotExist:
,这避免了对
exists()
@Alasdair的额外查询。您能用try和except向我展示解决方案吗?,我试过了,但不起作用…使用此解决方案,我将request.user.id而不是request.user,我在分配前引用了局部变量“profile”。我已使用try.添加了一个答案,下面除外。是否有方法显示未登录用户的配置文件?我使用这个ppl谁没有登录。我必须在请求结束时添加id。用户是否打印请求。用户可能显示“匿名用户”是否打印请求。用户可能显示“匿名用户”是否也可以显示未登录用户的配置文件?我使用这个ppl谁没有登录。我必须在请求结束时添加id。用户我已经用一个例子更新了我的答案来处理匿名用户,但我不确定这是否是你想要的。有没有一种方法可以同时显示未登录用户的个人资料?我使用这个ppl谁没有登录。我必须在请求结束时添加id。userI我已经用一个例子更新了我的答案来处理匿名用户,但我不确定这是否是你想要的。