如何显示自己的订单列表



我想显示单个用户的所有订单。此代码运行良好。但问题是,当我注销时,会显示以下错误。甚至连上一页都没有。现在,我能做什么?

错误:

AttributeError at /
'AnonymousUser' object has no attribute 'user_frontend_order'
Request Method: GET
Request URL:    http://127.0.0.1:8000/
Django Version: 3.2.3
Exception Type: AttributeError
Exception Value:    
'AnonymousUser' object has no attribute 'user_frontend_order'
Exception Location: C:UsersDCLAppDataLocalProgramsPythonPython39libsite-packagesdjangoutilsfunctional.py, line 247, in inner
Python Executable:  C:UsersDCLAppDataLocalProgramsPythonPython39python.exe
Python Version: 3.9.5
Python Path:    
['D:\1_WebDevelopment\Business_Website',
'C:\Users\DCL\AppData\Local\Programs\Python\Python39\python39.zip',
'C:\Users\DCL\AppData\Local\Programs\Python\Python39\DLLs',
'C:\Users\DCL\AppData\Local\Programs\Python\Python39\lib',
'C:\Users\DCL\AppData\Local\Programs\Python\Python39',
'C:\Users\DCL\AppData\Roaming\Python\Python39\site-packages',
'C:\Users\DCL\AppData\Roaming\Python\Python39\site-packages\win32',
'C:\Users\DCL\AppData\Roaming\Python\Python39\site-packages\win32\lib',
'C:\Users\DCL\AppData\Roaming\Python\Python39\site-packages\Pythonwin',
'C:\Users\DCL\AppData\Local\Programs\Python\Python39\lib\site-packages']
Server time:    Sun, 13 Mar 2022 18:12:22 +0000
Traceback Switch to copy-and-paste view
C:UsersDCLAppDataLocalProgramsPythonPython39libsite-packagesdjangocorehandlersexception.py, line 47, in inner
response = get_response(request) …
▶ Local vars
C:UsersDCLAppDataLocalProgramsPythonPython39libsite-packagesdjangocorehandlersbase.py, line 181, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs) …
▶ Local vars
D:1_WebDevelopmentBusiness_Websitebusiness_appviews.py, line 16, in index
queryset = request.user.user_frontend_order.all() …

视图:

def index(request):
total_user = User.objects.count()-1
frontend_order_list = request.user.user_frontend_order.all()

context = {
"total_user":total_user,
"frontend_order_list":frontend_order_list
}
return render(request,'0_index.html',context)

型号:

class Frontend_Order(models.Model):
USer = models.ForeignKey(User,default=None,on_delete=models.CASCADE,related_name='user_frontend_order')
Service_Type = models.CharField(max_length=250, null=True)
Price = models.CharField(max_length=250, null=True)
def __str__(self):
return str(self.pk)+ str(".") + str(self.USer)

它将转到上一页。问题是,在评估索引的逻辑时,存在一个问题:它旨在找到用户的Frontend_Order对象,但request.user不是User对象,而是AnonymousUser对象。因此,您应该进行适当的检查:

def index(request):
total_user = User.objects.count()-1

ifrequest.user.is_authenticated:
frontend_order_list = request.user.user_frontend_order.all()
else:
frontend_order_list = Frontend_Order.objects.none()

context = {
'total_user': total_user,
'frontend_order_list': frontend_order_list
}
return render(request, '0_index.html', context)

注意:通常最好使用settings.AUTH_USER_MODEL[Django-doc]来引用用户模型,而不是直接使用User模型Django-dok]分。


注意:Django中的模型是用PascalCase编写的,而不是用snake_case,因此,您可能希望将模型从Frontend_Order重命名为FrontendOrder

最新更新