我一直尝试在我的DetailView
中使用多个views.py
,但是由于某些原因,在模板渲染期间出现此错误。 http://prntscr.com/7f8vql
http://prntscr.com/7f8vlv。
我的views.py
def rock_and_feat(request):
feats = Feat.objects.order_by('-created')[:3]
rocks = Rockinfo.objects.order_by('-rank')[:50]
context = RequestContext(request, {
'feats': feats, 'rocks': rocks
})
return render_to_response('template.html', context)
class DetailView(generic.DetailView):
model = Feat
template_name = 'feature/detail.html'
context_object_name = 'feat'
class RockDetailView(generic.DetailView):
model = Rockinfo
template_name = 'feature/detailrock.html'
context_object_name = 'rockinfo'
我的应用程序
urls.py
urlpatterns = [
url(r'^$', views.rock_and_feat, name='rock_and_feat'),
url(r'^(?P<pk>[0-9]+)/$', views.DetailView.as_view(), name='detail'),
url(r'^(?P<pk>[0-9]+)/$', views.RockDetailView.as_view(), name='detailrock'),
]
我项目的
urls.py
urlpatterns = [
url(r'^', include('feature.urls', namespace="feature")),
url(r'^admin/', include(admin.site.urls)),
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
template.html
{% block rocker %}
{% if rocks %}
{% for item in rocks %}
<div class="artist-container">
<a href="{% url 'feature:detailrock' rockinfo.id %}"><img src="{{ item.rock_img.url }}" class="artist-img" alt="Dimitri Vegas & Like Mike">
<div class="artist-info">{{ item.rock_name }}</div></a></div>
{% endfor %}
{% else %}
<p>No rockers are available.</p>
{% endif %}
</div></div>
{% endblock %}
我不知道错误是什么。谢谢
最佳答案
我谨记您关于rock_and_feat
的问题。
我想提到几件事:
urlpatterns = [
url(r'^$', views.rock_and_feat, name='rock_and_feat'),
url(r'^detail/(?P<pk>[0-9]+)/$', views.DetailView.as_view(), name='detail'),
url(r'^rock_detail/(?P<pk>[0-9]+)/$', views.RockDetailView.as_view(), name='detailrock'),
]
您没有
rockinfo
。它是item
。在这一行:{% url 'feature:detailrock' item.id %}
还有一件事。注意,
id
is not the same为pk
。{% url 'feature:detailrock' item.pk %}
最后一件事。可以替换为:
{% if rocks %}
{% for item in rocks %}
....
{% endfor %}
{% else %}
<p>No rockers are available.</p>
{% endif %}
与此(docs):
{% for item in rocks %}
...
{% empty %}
<p>No rockers are available.</p>
{% endfor %}
关于python - Django 1.8使用多个DetailView,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/30749840/