【发布时间】:2020-10-22 12:45:04
【问题描述】:
为什么我得到错误 { TypeError at /4 'Model_name' object is not iterable } 对于我的这段代码
destinations.html
{% for dest in dests %}
<div>{{dest.name}}</div>
{% endfor %}
urls.py
我不知道我错了什么
urlpatterns=[
path('',views.index,name='index'),
path('<int:destinations_id>', views.destinations, name='destinations'),
]
views.py
当我通过“dests=Destination.objects.all()”获取模板上的所有模型行时,相同的“destinations.html”没有错误,但我只想要模板上的特定 id 行,所以我我正在使用“get_object_or_404”。
from django.shortcuts import get_object_or_404, render
from .models import Destination
def destinations(request, destinations_id):
dests = get_object_or_404(Destination, pk=destinations_id)
return render(request, 'destinations.html', {'dests':dests})
【问题讨论】:
标签: django django-models django-rest-framework django-templates