Can Django template {% load %} take a variable - django

Is it possible for the django template {% load %} tag to take a variable. I'm trying to pass the name of the tag set from my view to the template html and using it like this: {% load {{filter}} %} where filter is the name of the variable with the tag set name string. However, doing so is giving me the error: {{' is not a registered tag library. Must be one of:
I want to load some tag sets dynamically since this template file is part of a reusable app and the tag set will depend on which application is using this app. Thanks in advance!

In your template
{% if condition_from_view_1 %}
{% upload tag_library1 %}
{% elif condition_from_view_2 %}
{% upload tag_library2 %}
{% endif %}
Similarly, you can take the check down to the tag level.

Related

django template, using {% with %} to set a list var

I need to hard set a list in django template.
I know that I have to pass variables to the template, instead of creating them in the template, but I only have access to the template file. I'm using sendinblue with a custom template, and the only way to use custom params injected to the template is to use their api. I only need to hardcode some content in a list, and the content will dynamically appear depending on contact, I think that using an api only for this is overkill.
{% with tt='123'|make_list %}
{{ tt }}
<hr>
{% for x in tt %}
{{ x }}<br>
{% endfor %}
{% endwith %}
try this

Static template tag for Django template engine

I have the following code:
<img src="{% static 'images/{{i.sideid.sidepic}}' %}"/>
But this doesn't load the picture...
However, if I change the {{i.sideid.sidepic}} to the picture name "republic.png" it works tho. So, yeah, {{i.sideid.sidepic}} is actually the exact same name ("republic.png"), because I do a print in django views and shows it in my cmd, the exact same name "republic.png".
I guess there has to be a specific way to add that {{i.sideid.sidepic}} inside the jinja {% %} .
You should be able to concatenate strings with the add template filter:
{% with 'images/'|add:i.sideid.sidepic as image %}
{% static image %}
{% endwith %}
N.B. The variable directly following the as can be anything you want:
{% with 'images/'|add:i.sideid.sidepic as sidepic %}
{% static sidepic %}
{% endwith %}
What you are trying to do doesn't quite work with the static template tag because it takes either a string or a variable.
Hope that helps!

Django template tag a model object.

I'm trying to return a different value for my model object rather than the information stored in the field.
I'm running this in my html file. I've passed a query of 'localcampaigns' to my html file.
In my HTML file I have:
{% for campaign in localcampaigns %}
{{campaign.title}}
{{campaign.time}}
{{campaign.event_date}}
{{campaign.project_focus}}
{% endfor %}
So specifically, say I run this and for the {{campaign.project_focus}} I receive the database object of 'community001' - I want to take this and return something different than this 'community001' like "Community Project"
I've tried to do this by:
{% if '{{campaign.project_focus}}' == 'community001' %}
Community Project
{% endif %}
But I'm unsuccessful. whenever I run != in the template tag, I get the response. So I know that the two don't match. How do I make the two match? Thanks.
{% if campaign.project_focus == 'community001' %}
Community Project
{% endif %}
campaign.project_focus without {{ }} because it's recognized inside the for loop

Optimizing pattern of loading Django template filters in a for loop

In a Django template, I use a for loop to display content.
This content is of 3 types. Each type has its own set of conditions, HTML and CSS.
I felt including all that processing in 1 template would make it tough to maintain the file. So instead I'm using template tags:
{% load get_display_type_1 %}
{% load get_display_type_2 %}
{% load get_display_type_3 %}
{% for item in content %}
{% if item == '1' %}
{% display_type_1 payload='foo' %}
{% elif item == '2' %}
{% display_type_2 payload='bar' %}
{% elif item == '3' %}
{% display_type_3 payload='foo bar' %}
{% endif %}
{% endfor %}
And where, for example, the template tag called display_type_1 has the following code:
from django import template
register = template.Library()
#register.inclusion_tag(file_name='display_type_1.html')
def display_type_1(payload):
return {'payload':payload}
Standard stuff so far.
But now take a look at the HTML template connected to the template tag:
{% load myfilter %}
<div class="type1">{{ payload|myfilter }}</div>
I.e. notice I'm loading a custom filter called myfilter here.
So here's the problem: in essence, I've called {% load myfilter %} within a for loop. How? Because the template tag itself resides in the parent template's for loop.
This has slower performance then if I had written everything in the parent template and loaded the filter once.
I don't ideally want to abandon using template tags in this way; I find it more maintainable. But I don't want to sacrifice performance by loading filters (etc.) in a for loop either.
Can someone help me improve this pattern, making it more optimal? An illustrative example would be great.
Note: this is a simplified example, the actual code is more complex

Why is this django template tag failing to display?

I have a template tag located in catalog/templatetags/catalog_tags.py, which looks like this:
register = template.Library()
#register.inclusion_tag("tags/navigation.html")
def nav_links():
flatpage_list = FlatPage.objects.all()
return {'flatpage_list': flatpage_list }
I have a catalog.html which has {% load catalog_tags %}, to load that tag, and is followed by an inclusion tag for my navigation, {% include "tags/navigation.html" %}.
navigation.html contains the following:
{% with flatpage_list as pages %}
{% for page in pages %}
{{ page.title }}
{% endfor %}
{% endwith %}
But the list of flat_pages is not appearing in my navigation section. Why is that?
If I understand right, with your current state you have something liek this in catalog.html template:
{% load catalog_tags %}
.....
{% include "tags/navigation.html" %}
What this code does, is just renders the "tags/navigation.html" template, nothing more. So your custom template tag is not hit at all. To fix it, you should replace include with nav_links:
{% load catalog_tags %}
.....
{% nav_links %}
See Django docs for reference.
Not sure if it's just a copy paste error or not but return {'flatpage_list': flatpage_list isn't closed properly return {'flatpage_list': flatpage_list}
Also could this be something more suited for a context processor?
EDIT: After reading the other answer, I realized what you are trying to do, when you were using the {% include ... %} tag it seemed like you just wanted to populate the flatpage_list