yung peso
yung peso

Reputation: 1766

Django/Wagtail - How to create a conditional in template that checks for url path?

I'm struggling to finding a quick and easy solution to show HTML content based on a conditional that checks if the wagtail page is at root, aka '/'.

According to the Wagtail docs, I can also make this work using original request object from Django:

Additionally request. is available and contains Django’s request object.

This is essentially what I want to do. I use the page object here from wagtail but it might be better if I used the request object from Django instead. How can I


{% if page.request.path == '/' %}

  <div> show something </div>

{% else %}

   #show nothing

{% endif %}

How can I structure a conditional to solve what I'm trying to do?

Upvotes: 0

Views: 616

Answers (1)

LB Ben Johnston
LB Ben Johnston

Reputation: 5186

Access request

The request object can be accessed via request and not page.request.

A helpful tip is to add {% debug %} to see ALL the context that is available to the current template while working locally and debugging.

{% if request.path == '/' %}

  <div> show something </div>

{% else %}

   #show nothing

{% endif %}

More info

Within Wagtail the request object should be available to all templates, however you may need to enable this by following the instructions about Using RequestContext in the Django docs.

Alternatively this Django request in template answer provides a clear example of what to update in your settings file.

Upvotes: 1

Related Questions