Omar Gonzales
Omar Gonzales

Reputation: 4008

Django: check for exact url in request using regex

I need to check if the url is exactly:

http://127.0.0.1:8000/shop/

and based on this render a header.

If it is, for example:

http://127.0.0.1:8000/shop/stickers/stickers-por-hoja/medida-y-cantidad

Header shouldn't render.

I've read you can do something like this:

{% if "/shop/$" in request.path %}
    {% include 'header.html' %}
{% endif %}

But this doesn't work. On the other hand this works, but is not what I need:

{% if "/shop/" in request.path %}
     {% include 'header.html' %}
{% endif %}

So, Can I use regex in the if condition?

Upvotes: 0

Views: 377

Answers (3)

Karim N Gorjux
Karim N Gorjux

Reputation: 3033

Create a template tag to do that.

The url whitelisted must be somewhere in the settings. For example you can add something like this

HEADER_WHITELIST_URLS = (
     'regex1',
     'regex2',
)

2) Your template tags will check the current URL and if one of the regex matches, you can render your header. In a hypothetical version2, you can also add the header to show for a specific regex.

Use this snippet as starting point for your template tag

import datetime
from django import template
from django.conf import settings

register = template.Library()

@register.simple_tag(takes_context=True)
def render_header(context, format_string):
    regexs = settings.HEADER_WHITELIST_URLS

    # maybe you can add some try/exception if the settings is not available.

    for regex in regexs:
        # test the regex with the url in the context.
        # if matches return the code to render the header and break
    return None

Check here for more details about the template tags: https://docs.djangoproject.com/en/2.1/howto/custom-template-tags/#writing-custom-template-tags

Upvotes: 1

Saiful Azad
Saiful Azad

Reputation: 1911

Create a context processor has_shop_in_url.py having this code below.

import re
def has_shop(request):
    shop_in_request = your_regex_validation

    return {
        'has_shop_in_url':   any([shop_in_request])  }

Add that context processor

TEMPLATES = [
    {
        .....
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
                'your_module.has_shop_in_url.has_shop' # YOUR PROCESSOR
            ],
        },
    },
]

Now it is available for your whole web application. In template just write {{has_shop_in_url}}. Your can see True/False.

Upvotes: 1

A. J. Parr
A. J. Parr

Reputation: 8026

You could move the regex check into the view and add a field to the context for the template like so:

class MyView(View):

    def action(self, request, *args, **kwargs):
        shop_in_request = re.findall(r"/shop/$", request.path)
        context = {"include_header": any(shop_in_request)}
        return render(template, context)

Then in your view you could use:

{% if include_header %}
    {% include 'header.html' %}
{% endif %}

Upvotes: 1

Related Questions