Django - Did you forget to register or load this tag?

前端 未结 9 2087
我寻月下人不归
我寻月下人不归 2020-12-30 00:04

I\'ve created a custom tag that I want to use, but Django can\'t seem to find it. My templatetags directory is set up like this:

pygmentize

9条回答
  •  难免孤独
    2020-12-30 00:29

    I had the same problem, here's how I solved it. Following the first section of this very excellent Django tutorial, I did the following:

    1. Create a new Django app by executing: python manage.py startapp new_app
    2. Edit the settings.py file, adding the following to the list of INSTALLED_APPS: 'new_app',
    3. Add a new module to the new_app package named new_app_tags.
    4. In a Django HTML template, add the following to the top of the file, but after {% extends 'base_template_name.html' %}: {% load new_app_tags %}
    5. In the new_app_tags module file, create a custom template tag (see below).
    6. In the same Django HTML template, from step 4 above, use your shiney new custom tag like so: {% multiply_by_two | "5.0" %}
    7. Celebrate!

    Example from step 5 above:

    from django import template
    
    register = template.Library()
    
    @register.simple_tag
    def multiply_by_two(value):
        return float(value) * 2.0
    

提交回复
热议问题