'Post' 对象不可迭代,即使我正在使用过滤器
'Post' object is not iterable even though i'm using filter
我正在尝试获取特定车辆的所有帖子,这是我拥有的代码:
*vehicles/views.py*
class UserVehicleListView(ListView):
model = Vehicle
template_name = 'vehicles/vehicles.html' # <app>/<model>_<viewtype>.html
context_object_name = 'vehicles'
def get_queryset(self):
print(Vehicle.objects.filter(owner_id= self.request.user.id))
return Vehicle.objects.filter(owner_id= self.request.user.id)
class UserVehicleDetailView(DetailView):
model = Post
template_name = 'vehicles/vehicle_detail.html' # <app>/<model>_<viewtype>.html
context_object_name = 'posts'
def get_queryset(self):
vehicle = get_object_or_404(Vehicle, id =self.kwargs.get('pk'))
print(Post.objects.filter(vehicle= vehicle))
return Post.objects.filter(vehicle= vehicle)
*vehicles/urls.py*
urlpatterns = [
path('vehicles/', UserVehicleListView.as_view(), name='vehicle-list'),
path('vehicles/<int:pk>/', UserVehicleDetailView.as_view() , name='vehicle-detail'),
]
urlpatterns += staticfiles_urlpatterns()
*vehicles/templates/vehicles/vehicle.html*
{% for vehicle in vehicles %}
<div class="col-lg-6 mb-3 mb-lg-0">
<div class="hover-overlay" ></div>
<div class="hover-1-content px-5 py-4">
<a href=" {%url 'vehicle-detail' vehicle.id %}">
<h3 class="hover-1-title text-uppercase font-weight-bold mb-0"> <span class="font-weight-light">Volvo </span></h3>
</a>
<p class="hover-1-description font-weight-light mb-0">Lorem ipsum dolor sit amet, consectetur adipisicing elit.</p>
</div>
{%endfor%}
*vehicles/templates/vehicles/vehicle_detail.html*
{% extends "blog/base.html" %}
{% block content %}
<article class="media content-section">
<h2>
{% for post in posts %}
{{post.date_posted}}
{%endfor%}
</h2>
</article>
{% endblock content %}
当我使用 Vehicle.objects.filter(owner_id= self.request.user.id) 打印时,我进入命令:
, ]>
但是当我在 vehicle_detail.html 中使用 for 循环传递值时,我得到:
** /vehicles/1/'Post' 对象的类型错误不可迭代**
当我通过 {{object.date_posted}} 我只得到 1 Post,我如何通过所有帖子?在此先感谢您的帮助!
发现了问题,我传递的是 DetailView 而不是 ListView
我正在尝试获取特定车辆的所有帖子,这是我拥有的代码:
*vehicles/views.py*
class UserVehicleListView(ListView):
model = Vehicle
template_name = 'vehicles/vehicles.html' # <app>/<model>_<viewtype>.html
context_object_name = 'vehicles'
def get_queryset(self):
print(Vehicle.objects.filter(owner_id= self.request.user.id))
return Vehicle.objects.filter(owner_id= self.request.user.id)
class UserVehicleDetailView(DetailView):
model = Post
template_name = 'vehicles/vehicle_detail.html' # <app>/<model>_<viewtype>.html
context_object_name = 'posts'
def get_queryset(self):
vehicle = get_object_or_404(Vehicle, id =self.kwargs.get('pk'))
print(Post.objects.filter(vehicle= vehicle))
return Post.objects.filter(vehicle= vehicle)
*vehicles/urls.py*
urlpatterns = [
path('vehicles/', UserVehicleListView.as_view(), name='vehicle-list'),
path('vehicles/<int:pk>/', UserVehicleDetailView.as_view() , name='vehicle-detail'),
]
urlpatterns += staticfiles_urlpatterns()
*vehicles/templates/vehicles/vehicle.html*
{% for vehicle in vehicles %}
<div class="col-lg-6 mb-3 mb-lg-0">
<div class="hover-overlay" ></div>
<div class="hover-1-content px-5 py-4">
<a href=" {%url 'vehicle-detail' vehicle.id %}">
<h3 class="hover-1-title text-uppercase font-weight-bold mb-0"> <span class="font-weight-light">Volvo </span></h3>
</a>
<p class="hover-1-description font-weight-light mb-0">Lorem ipsum dolor sit amet, consectetur adipisicing elit.</p>
</div>
{%endfor%}
*vehicles/templates/vehicles/vehicle_detail.html*
{% extends "blog/base.html" %}
{% block content %}
<article class="media content-section">
<h2>
{% for post in posts %}
{{post.date_posted}}
{%endfor%}
</h2>
</article>
{% endblock content %}
当我使用 Vehicle.objects.filter(owner_id= self.request.user.id) 打印时,我进入命令:
** /vehicles/1/'Post' 对象的类型错误不可迭代**
当我通过 {{object.date_posted}} 我只得到 1 Post,我如何通过所有帖子?在此先感谢您的帮助!
发现了问题,我传递的是 DetailView 而不是 ListView