[Answer]-Template tag is not working as intended in if-sentence

1πŸ‘

βœ…

here is what you have to do:

instead of your templatetag add this in templatetags/my_tag.py

from django import template

register = template.Library()

class IsProductionAreaNode(template.Node):
    def __init__(self, nodelist):
        self.nodelist = nodelist

    def render(self, context):
        if settings.SETTINGS_MODE == 'Production':
            return self.nodelist.render(context)
        else:
            return ''


def do_is_production(parser, token):
    nodelist = parser.parse(('endis_production',))
    parser.delete_first_token()
    return IsProductionAreaNode(nodelist)

register.tag('is_production', do_is_production)

now in your template you do following:

{% load my_tag %}
.
.
.

{% is_production %}
    *content*
{% endis_production %}
πŸ‘€Sasa

0πŸ‘

Here is a common snippet: https://djangosnippets.org/snippets/1538/.

But in your case I recommend to set context variable is_production in the view.

πŸ‘€madzohan

Leave a comment