skip to Main Content

I need to do something like this:
{% load static %} {% include {% static path %} %}

I have a lot of files, and I give this path variable in context, and it also contains in static directory, so I cant just do
{% include path %}

Also I found that I cant just put function inside another function: {% %} inside {% %}
I looked at the Jinja’s documention to find how to deal with this inside functions, but that didnt help me.

2

Answers


  1. Chosen as BEST ANSWER

    I just moved all files to templates. Idk why I wanted to store it all in static


  2. I would recommend that you treat it with some custom filters or custom tags.
    But I guess this would work for your case.

    {% load static %}
    {% with static_path=static path %}
      {% include static_path %}
    {% endwith %}
    

    from django import template
    from django.templatetags.static import static
    
    register = template.Library()
    
    @register.filter
    def include_static(path):
        return static(path)
    

    And then

    {% load custom_filters %}
    
    {% with static_path=path|include_static %}
      {% include static_path %}
    {% endwith %}
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search