How to get the url of the root page in template in Wagtail?
Asked Answered
H

6

6

pageurl can do it with a Page object, but I am not sure how to get the root page object.

Harrus answered 11/7, 2018 at 22:48 Comment(0)
M
11

The URL of the root page is /. Just write <a href="/"> - no need to use a template tag.

(You might think that's cheating, but it's literally the definition of the root page.)

If you have your include(wagtail_urls) line in urls.py rooted at a path other than /, and don't want to hard-code that path in your template, you can use: {% url 'wagtail_serve' '' %} (note the empty string after 'wagtail_serve').

Mic answered 11/7, 2018 at 23:3 Comment(6)
Sorry I didn't make it clear. I meant the root page defined under Settings in Wagtail CMS. Not the root page of the whole site.Harrus
You mean the one you select under Settings -> Sites? Yes, that's the one I'm talking about too. When you select a page there, you are choosing which page you want to appear at the URL "/".Mic
Or If I do it in Django way, ie. {% url 'name_of_wagtail_root_view' %}, I want to know what that name is. My url setting: re_path(r'^blog/', include(wagtail_urls)), I tried {% url 'wagtail_serve' %}, but it gives error: Reverse for 'wagtail_serve' with no arguments not found. 1 pattern(s) tried: ['blog/((?:[\\w\\-]+/)*)$']Harrus
The problem is "/" is not the root of my site. Please see the above comment. Thanks.Harrus
OK, I understand now - please see my updated answer.Mic
wagtail_urls seems to be deprecated. See updated answer with new tags tag.Sponger
D
6

If you use wagtail_modeltranslation and want to get the translated root page url (eg: example.com/fr/ for French), you can use {% pageurl request.site.root_page %}.

For wagtail versions > 2.9:

{% load wagtailcore_tags %}
{% wagtail_site as current_site %}

Then either use {% pageurl current_site.root_page %} or {{ current_site.root_url }}.

Delphadelphi answered 5/8, 2018 at 9:26 Comment(0)
B
5

To show the site root page in a wagtail template, add the following to your page template:

{% load wagtailcore_tags %}
<a href="{{ request.site.root_url }}">Site Url</a>

This code is deprecated. Starting with wagtail 2.9, the preferred option is {% wagtail_site %}. View wagtail 2.9 release notes

Brougham answered 13/7, 2018 at 14:31 Comment(2)
This doesn't work: "This site can’t be reached localhost refused to connect."Gasp
@danny you should try {{ page.get_site.root_url }}Brougham
A
3

With wagtail version > 2.12.2 you need to import {% wagtail_site as current_site %}

and then you can use: {{ current_site.root_url }}in your template.

Ardithardme answered 28/3, 2021 at 10:58 Comment(0)
C
2

If you use wagtail-localize this will get you the homepage for your current locale

{% pageurl page.get_site.root_page.localized %}

Carnation answered 20/9, 2022 at 21:31 Comment(0)
S
0

The most recent version of Wagtail, 5.1.1, you can use the {% url 'wagtail_serve' '' %} template tag.

For example, in html it would be written:

<a class="footer-links" href="{% url 'wagtail_serve' '' %}">Blog</a>
Sponger answered 5/9, 2023 at 22:0 Comment(2)
It's not necessary to load wagtailcore_tags before using the {% url %} tag, because {% url %} is a standard tag built into Django, not part of Wagtail. (You might have other things on the template that require wagtailcore_tags, though.)Mic
You're correct. I've updated this. The accepted answer is correct, and mentions the need for the need for include(wagtail_urls).Sponger

© 2022 - 2025 — McMap. All rights reserved.