How to list wagtail pages in Django template?

◇◆丶佛笑我妖孽 提交于 2019-12-11 15:00:48

问题


I have a site that is mostly made with Django but has a few content pages that need to be managed by Wagtail.

How can I link to the pages created with Wagtail in Django templates? I don't want to have to hardcode any of the pages. The client should be able to make a page in Wagtail and a link should be created to that page in Django templates.

The problem is that I don't know how to reference Wagtail pages through Django templates.


回答1:


When you say

reference Wagtail pages through Django templates

I assume you mean render the navigation that will take the user to a page if they click on a navigation item. To render the navigation for the page tree, in the a template file (typically base.html) you could do:

{% for item in request.site.root_page.get_children.live.in_menu %}
    <a href="{% pageurl item %}">{{ item.title }}</a>
{% endfor %}

The above is the simplest rendering of page navigation. There are a number of other considerations you will likely run into when building navigation, one of which is building nested navigation. I could also post about this if you need that information (I build unlimited nested navigation through a recursive call to a small fragment of html in a separate file from base.html).



来源:https://stackoverflow.com/questions/59142970/how-to-list-wagtail-pages-in-django-template

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!