我有一个 wagtail 项目,根目录有一个“主页”(slug='home')。
第二页“博客”是“主页”的子页面(slug='blog')。
最后,我有博客发布页面,它们是“博客”的子页面(slug='blog-post-1' 等)。
在blog/models.py我有以下代码:
class BlogListingPage(Page):
"""List the Blog detail pages."""
template = "blog/blog_listing_page.html"
....
def get_context(self, request, *args, **kwargs):
"""Add custom content to our context."""
context = super().get_context(request, *args, **kwargs)
context['blog_pages'] = self.get_children().live()
return context
class BlogDetailPage(Page):
"""Blog detail page."""
template = "blog/blog_detail_page.html"
....
Run Code Online (Sandbox Code Playgroud)
要访问我使用的博客:
<a class="nav-link" href="/blog">Blog</a>
Run Code Online (Sandbox Code Playgroud)
这非常有效。
现在,详细信息页面的 URL 是/home/blog/blog-post-1/等,但该页面实际上位于/blog/blog-post-1/.
'/home' 来自哪里,我该如何摆脱它?
您正在使用{{ page.url_path }}. 这是Page对象的内部字段,不打算直接用作页面 URL(因为这取决于站点根目录的放置位置,在设置 -> 站点下) - 相反,Wagtail 计算最终 URL作为目标页面url_path(/home/blog/blog-post-1/此处)和根页面url_path(/home/)之间的差异。
指定一个模板的页面URL正确的方法是:{% pageurl page %}。(您还需要{% load wagtailcore_tags %}在模板的顶部。)
| 归档时间: |
|
| 查看次数: |
203 次 |
| 最近记录: |