gpt4 book ai didi

python - Django 1.8 使用多个 DetailView

转载 作者:行者123 更新时间:2023-11-30 23:10:18 24 4
gpt4 key购买 nike

我一直在尝试在我的 views.py 中使用多个 DetailView,但由于某种原因,我在模板渲染期间收到此错误。 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 &amp; 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com