为什么 Django 会抛出类似“'function' object has no attribute 'order_set'”这样的错误?
Why django throwing error like " 'function' object has no attribute 'order_set' "?
我编辑了 views.py,然后它抛出了如下截图所示的错误。
这是代码。
/apps/views.py 包含
from django.shortcuts import render
from django.http import HttpResponse
from .models import *
def customer(request, pk):
customers = Customer.objects.get(id=pk)
order = customer.order_set.all()
context={'customers':customers, 'order':order}
return render(request, 'accounts/customer.html',context)
和 /templates/apps/name。html 包含此代码以将数据从模型渲染到模板。
{% for i in order %}
<tr>
<td>{{i.product}}</td>
<td>{{i.product.category}}</td>
<td>{{i.date_created}}</td>
<td>{{i.status}}</td>
<td><a href="">Update</a></td>
<td><a href="">Remove</a></td>
</tr>
{% endfor %}
我认为这个错误与 views.py 中的 order_ser 有关,但我不确定如何修复它。
你写了customers = customer.objects.get(id=pk)
,然后你用了customer.order_set
,这里的customer
指的是customer
函数。你应该使用 customer
:
from django.shortcuts import get_object_or_404, render
from django.http import HttpResponse
from .models import *
def customer(request, pk):
# ↓ without s
<strong>customer</strong> = get_object_or_404(Customer, id=pk)
order = <strong>customer.</strong>order_set.all()
context={'customer': customer, 'order':order}
return render(request, 'accounts/customer.html', context)
Note: It is often better to use get_object_or_404(…)
[Django-doc],
then to use .get(…)
[Django-doc] directly. In case the object does not exists,
for example because the user altered the URL themselves, the get_object_or_404(…)
will result in returning a HTTP 404 Not Found response, whereas using
.get(…)
will result in a HTTP 500 Server Error.
我编辑了 views.py,然后它抛出了如下截图所示的错误。
这是代码。
/apps/views.py 包含
from django.shortcuts import render
from django.http import HttpResponse
from .models import *
def customer(request, pk):
customers = Customer.objects.get(id=pk)
order = customer.order_set.all()
context={'customers':customers, 'order':order}
return render(request, 'accounts/customer.html',context)
和 /templates/apps/name。html 包含此代码以将数据从模型渲染到模板。
{% for i in order %}
<tr>
<td>{{i.product}}</td>
<td>{{i.product.category}}</td>
<td>{{i.date_created}}</td>
<td>{{i.status}}</td>
<td><a href="">Update</a></td>
<td><a href="">Remove</a></td>
</tr>
{% endfor %}
我认为这个错误与 views.py 中的 order_ser 有关,但我不确定如何修复它。
你写了customers = customer.objects.get(id=pk)
,然后你用了customer.order_set
,这里的customer
指的是customer
函数。你应该使用 customer
:
from django.shortcuts import get_object_or_404, render
from django.http import HttpResponse
from .models import *
def customer(request, pk):
# ↓ without s
<strong>customer</strong> = get_object_or_404(Customer, id=pk)
order = <strong>customer.</strong>order_set.all()
context={'customer': customer, 'order':order}
return render(request, 'accounts/customer.html', context)
Note: It is often better to use
get_object_or_404(…)
[Django-doc], then to use.get(…)
[Django-doc] directly. In case the object does not exists, for example because the user altered the URL themselves, theget_object_or_404(…)
will result in returning a HTTP 404 Not Found response, whereas using.get(…)
will result in a HTTP 500 Server Error.