Why is my template tag in Django not working?

{% gen_aws "hello" %}

In my file, I do this:

#I want to add "goodbye" to every word passed to this tag.
@register.tag(name="gen_aws")
def gen_aws(s):
    return s + "goodbye"

The .py file is fine ... I include everything in order. I have other "filter" templates that work fine. But then I added this to this file, and this template tag does not work.

+3
source share
2 answers

Your description of "not working" is not very accurate (more precisely, it does not exist). But I think you will get an error because the tag was not found.

The documentation clearly states that you need the "templatetags" module in your application, for example, a submodule, such as "mytags". Then you must include these tags in each template that you want to use. You can do this with {% load mytags %}.

"mytags" "gen_aws" .

: "gen_aws() 1 (2 )" , . "" "" . - Django Python. @register.tag @register.simple_tag.

+9

"no attribute 'must_be_first'" , django.template.Node . ( Google , , , .)

+5

Source: https://habr.com/ru/post/1791167/


All Articles