M
M
Maxim Zubenko2021-12-01 13:36:08
Django
Maxim Zubenko, 2021-12-01 13:36:08

How to use custom Django tags in If-else-endif template constructs?

The bottom line is:
I added my own parameter to the settings.py file:

MY_PARAMETR = True

created a templatetags folder with the __init__.py file and I made a custom tag:
(in the my_tag.py file)

from django import template
from ws.settings import MY_PARAMETR

register = template.Library()

@register.simple_tag
def get_my_parametr():
    return MY_PARAMETR


Now I want to use this construction in Django templates:
{% load my_tag %}

{% if get_my_parametr %}
    {% include "template_true.html" %}
{% else %}
    {% include "template_false.html" %}
{% endif %}


and it doesn't work.

tried this:
{% if get_my_parametr == 'True' %}
and this:
{% if get_my_parametr == True %}

still doesn't work.

How to make it possible to use your own custom tag in the template as a parameter for branching the If-else-endif construct?

ps It's just that the content of the tag is in the template (I see it in the browser), i.e. if I write:
<h1>{% get_my_parametr %}</h1>
then I perfectly see the inscription True

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey Gornostaev, 2021-12-01
@JawsIk

{% get_my_parameter as my_parameter %}

{% if my_parameter %}
    ...
{% endif %}

PS It is better to load the configuration like thisfrom django.conf import settings

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question