timkung
timkung

Reputation: 99

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

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

Upvotes: 6

Views: 11819

Answers (6)

Rob
Rob

Reputation: 1919

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>

Upvotes: 0

Nhat Cuong Nguyen
Nhat Cuong Nguyen

Reputation: 156

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 }}.

Upvotes: 6

Thanasis Xanthopoulos
Thanasis Xanthopoulos

Reputation: 23

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

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

Upvotes: 2

Lukas Seyfarth
Lukas Seyfarth

Reputation: 184

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.

Upvotes: 3

jcuot
jcuot

Reputation: 951

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

Upvotes: 5

gasman
gasman

Reputation: 25317

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').

Upvotes: 12

Related Questions