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

前端 未结 9 2066
我寻月下人不归
我寻月下人不归 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

    For Django 2.2 up to 3, you have to load staticfiles in html template first before use static keyword

    {% load staticfiles %}
    <link rel="stylesheet" href="{% static 'css/bootstrap.min.css' %}">
    

    For other versions use static

    {% load static %}
    <link rel="stylesheet" href="{% static 'css/bootstrap.min.css' %}">
    

    Also you have to check that you defined STATIC_URL in setting.py

    At last, make sure the static files exist in the defined folder

    0 讨论(0)
  • 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
    
    0 讨论(0)
  • 2020-12-30 00:30

    did you try this

    {% load games_tags %} 
    

    at the top instead of pygmentize?

    0 讨论(0)
提交回复
热议问题