我需要编写for循环来创建一个菜单,为每个创建的页面添加项目标题添加到菜单中,我认为这样可以解决问题:
menu.html:
{% block menu %}
<ul>
{% for page in pages %}
<a href="{{page.url}}">{{title}}</a>
{% endfor %}
</ul>
{%endblock%}
来自 base.html:
{% block menu %}
{% include "menu.html" %}
{% endblock %}
来自 about.html:
{% block menu %}
{% include "menu.html" %}
{% endblock %}
view.py
def about(request):
about = Page.objects.get(title="About")
content = about.content
title = about.title
return render_to_response("about.html", {"content":content, "title":title})
model.py
class Page(models.Model):
title = models.CharField(max_length=30)
content = models.TextField()
但事实并非如此。 &lt; a href =&#34; {{page.url}}&#34;&gt; {{title}}&lt; / a&gt;显示当前页面的链接,但我无法弄清楚如何正确构建for循环。指针会很受欢迎。