Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

wagtail

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

like image 290
timkung Avatar asked Jul 11 '18 22:07

timkung


2 Answers

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

like image 200
gasman Avatar answered Oct 08 '22 19:10

gasman


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

like image 45
jcuot Avatar answered Oct 08 '22 20:10

jcuot