使用Django,是否可以在DetailView类页面的页面中包含For循环?
我有从我的数据库中提取的详细页面,但是我希望我可以在该页面内有一个For循环,该循环遍历该表中的名称列表以相互链接(如果有意义)。
urls.py:
from django.urls import path, include
from .views import payplan_emails_view
urlpatterns = [
path('normal-payplan-emails/<int:pk>', payplan_emails_view.as_view(), name="normal-payplan-emails"),
]
views.py:
from django.shortcuts import render, redirect
from .models import knowledgebase_instruction
from django.views.generic import DetailView
class payplan_emails_view(DetailView):
model = knowledgebase_instruction
template_name = "instructions/payplans_base.html"
payplans_base.html:
{% block content %}
<h4>{{knowledgebase_instruction.name}}</h4>
<p>{{knowledgebase_instruction.instructions|safe}}</p>
<ul>
<!-- I would love to loop through items in the table here -->
<li><a href="#">List Item 1</a></li>
</ul>
{% endblock%}
我走了一条不同的路。
而不是使用通用的DetailView。我只是在视图中创建了一个新函数,并在其中查询数据库,并从URL传递了pk。
现在运作良好。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句