Reputation: 3667
I have a wagtail project with a 'Home' page at root (slug='home').
There is a second page 'Blog' which is a child of 'Home' (slug='blog').
Finally, I have blog-posting pages which are children of 'Blog' (slug='blog-post-1' etc.).
In blog/models.py
I have the following code:
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"
....
To access the blog I use:
<a class="nav-link" href="/blog">Blog</a>
And that works perfectly.
Now, the URLs for the detail pages are /home/blog/blog-post-1/
etc, but the page is really located at /blog/blog-post-1/
.
Where is the '/home' coming from and how do I get rid of it?
Upvotes: 2
Views: 884
Reputation: 25227
You're outputting the page URL using {{ page.url_path }}
. This is an internal field of the Page
object, and isn't intended to be used directly as the page URL (because that will depend on where the site root has been placed, under Settings -> Sites) - rather, Wagtail calculates the final URL as the difference between the target page's url_path
(/home/blog/blog-post-1/
here) and the root page's url_path
(/home/
).
The correct way to specify a page URL in a template is: {% pageurl page %}
. (You'll also need {% load wagtailcore_tags %}
at the top of your template.)
Upvotes: 7
Reputation: 3098
Make sure you don't have '/home'
in urls.py
. If you do, replace it with ''
. The last line in urls.py
would typically be:
urlpatterns.append(path('', include(wagtail_urls)))
Also, in the Wagtail admin, in Settings -> Sites, make sure you've chosen the root page for your site.
Upvotes: 0