Here is what i want
tmpl1.jinja
{% for x in List %}
{% set User = List[x] %}
{% include 'tmpl2.jinja' %}
{% endfor %}
tmpl2.jinja
{% extends "tmpl3.jinja" %}
{% block link %}
<a>share</a>
{% endblock link %}
tmpl3.jinja
User.name
{% block link %}
{% endblock link %}
Basically i have a user block that exists across site with only the action(one or more link but with quiet a few html like image etc) changing. What can i do.
Thanks
For the template part everything looks absolutely ok and there should not be problem if what you do is what you showed.
Is Your list is dict() or actually list()?
Because your problem is here:
{% for x in List %}
{% set User = List[x] %}
This syntax will work only if List is dictionary.
In case of list you should write:
{% for x in List %}
{% set User = x %}
Related
{% if request.user.profile.emp_desi in qa_list %}
{% extends "qa_base.html" %}
{% elif request.user.profile.emp_desi in mgr_list %}
{% extends "manager_base.html" %}
{% else %}
{% extends "common_base.html" %}
{% endif %}
How can I solve this problem??
based on designation I want to extend different different bases.
You can use include
You need to create a directory and template layout in such a way that
you will have one base and multiple includes.
https://docs.djangoproject.com/en/4.0/ref/templates/builtins/#include
{% if request.user.profile.emp_desi in qa_list %}
{% include "qa_base.html" %}
{% elif request.user.profile.emp_desi in mgr_list %}
{% include "manager_base.html" %}
{% else %}
{% include "common_base.html" %}
{% endif %}
Interesting issue here: I have plain-text emails sent by my Django app, with the following code:
email_context = dict()
email_context['to'] = user.display_name
email_context['new'] = user_data['new']
email_context['for_reminder'] = user_data['for_reminder']
plaintext = get_template('email/notification.txt')
text_content = plaintext.render_to_string(email_context)
html = get_template('email/notification.html')
html_content = html.render(email_context)
email = EmailMultiAlternatives(
subject='Assignments',
body=text_content,
to=[user.email],
reply_to=[settings.DEFAULT_FROM_EMAIL],
)
# email.attach_alternative(html_content, "text/html")
email.send()
the 'email/notification_base.txt' template looks like such:
Dear {{ to }},
{% if new %}
You have been assigned to the following NEW cases:
{% block new %}
{% endblock %}
{% endif %}
{% if for_reminder %}
You still have the following previously assigned cases, which need your attention:
{% block for_reminder %}
{% endblock %}
{% endif %}
Thank you for your assistance.
Sincerely,
The Team
the 'email/notification.txt' template just extends base:
{% extends 'email/notification_base.txt' %}
{% load common_tags %}
{% load common_filters %}
{% block new %}
{% for dispute in new %}
Case #: {{ dispute.case_id.no}}
Assignment date: {{ dispute.assignment_date }}
{% endfor %}
{% endblock %}
{% block for_reminder %}
{% for dispute in for_reminder %}
Case #: {{ dispute.case_id.no }}
Assignment date: {{ dispute.assignment_date }}
{% endfor %}
{% endblock %}
My problem with it is, that the email it generates and sends to the addressee, actually gets rendered with a NEWLINE in every spot where the template had {% something %} tags. This results in a very unevenly formatted email text. As an example, if both blocks were missing the email would look like:
Dear Someone,
Thank you for your assistance.
Sincerely,
The Team
Are there any ways around this problem?
I have the list of objects to show on web pages(HTML file).
And the type(graph, table etc.) of objects is different from the situation.
If there is a graph objects, I should load js and css files about a graph.
Because I do not want to load js, css files for graph when there is no graph object in the list,
I have implemented the following jinja2 template HTML file.
{% block body %}
{% set has_graph = 0 %}
{% for item in components %}
{% if item.form == 'graph' %}
{% set has_graph = 1 %}
{% endif %}
{% endfor %}
{% if has_graph == 1 %}
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.6/d3.min.js"></script>
{% endif %}
{% endblock %}
I have found {% set has_graph = 1 %} worked, but the js file was not loaded.
I do not know why {% if has_graph == 1 %} does not work.
I found that the scope of set statement could not be beyond the loop in jinja2 document(http://jinja.pocoo.org/docs/2.9/templates/#id12).
Please keep in mind that it is not possible to set variables inside a block and have them show up outside of it. This also applies to loops. The only exception to that rule are if statements which do not introduce a scope. As a result the following template is not going to do what you might expect:
{% set iterated = false %}
{% for item in seq %}
{{ item }}
{% set iterated = true %}
{% endfor %}
{% if not iterated %} did not iterate {% endif %}
It is not possible with Jinja syntax to do this.
It is true that global variables are generally not within scope inside for loops in jinja, which is a surprise to many (users of Python, Java, etc).
However, the workaround is to declare a dictionary object in that outer scope, which is then available in the for-loop:
{% set foundItem = { 'found': False } %}
{% for item in seq %}
{%- if item.form == "graph" %}
{%- if foundItem.update({'found':True}) %} {%- endif %}
{%- endif %}
{% endfor %}
{% if not iterated %} did not iterate {% endif %}
{% if foundItem.flag %} pull in graph CSS/JS {% endif %}
Is it possible to set query parameters via jinja in Pelican template files?
index.html
{% if articles %}
{% for article in articles_page.object_list if article.category == 'article' %}
#stuff
{% endfor %}
{% endif %}
This will return articles in the article category, but only if they happen to be in the articles already queried for. The desirable setup would be to grab x articles in y category (or with y tag) - is that possible?
This code snippet works for me to bring back a list of all articles matching a tag:
{% block content %}
<ul>
{% for article in articles if FAVORITES_TAG in article.tags %}
{% if loop.index <= FAVORITES_COUNT %}
<li>{{ article.title }}</li>
{% endif %}
{% endfor %}
</ul>
{% endblock %}
I set the FAVORITES_TAG and FAVORITES_COUNT variables in the config. I hope that helps.
I ran into the same problem, and found a solution
{% if articles %}
{% for article in articles_page.object_list if article.category.name == 'article' %}
#stuff
{% endfor %}
{% endif %}
I am already trying to concatenate like this:
{% for choice in choice_dict %}
{% if choice =='2' %}
{% with "mod"|add:forloop.counter|add:".html" as template %}
{% include template %}
{% endwith %}
{% endif %}
{% endfor %}
but for some reason I am only getting "mod.html" and not the forloop.counter number. Does anyone have any idea what is going on and what I can do to fix this issue? Thanks alot!
Your problem is that the forloop.counter is an integer and you are using the add template filter which will behave properly if you pass it all strings or all integers, but not a mix.
One way to work around this is:
{% for x in some_list %}
{% with y=forloop.counter|stringformat:"s" %}
{% with template="mod"|add:y|add:".html" %}
<p>{{ template }}</p>
{% endwith %}
{% endwith %}
{% endfor %}
which results in:
<p>mod1.html</p>
<p>mod2.html</p>
<p>mod3.html</p>
<p>mod4.html</p>
<p>mod5.html</p>
<p>mod6.html</p>
...
The second with tag is required because stringformat tag is implemented with an automatically prepended %. To get around this you can create a custom filter. I use something similar to this:
http://djangosnippets.org/snippets/393/
save the snipped as some_app/templatetags/some_name.py
from django import template
register = template.Library()
def format(value, arg):
"""
Alters default filter "stringformat" to not add the % at the front,
so the variable can be placed anywhere in the string.
"""
try:
if value:
return (unicode(arg)) % value
else:
return u''
except (ValueError, TypeError):
return u''
register.filter('format', format)
in template:
{% load some_name.py %}
{% for x in some_list %}
{% with template=forloop.counter|format:"mod%s.html" %}
<p>{{ template }}</p>
{% endwith %}
{% endfor %}
You probably don't want to do this in your templates, this seems more like a views job: (use of if within a for loop).
chosen_templates=[]
for choice in choice_dict:
if choice =='2':
{% with "mod"|add:forloop.counter|add:".html" as template %}
template_name = "mod%i.html" %index
chosen_templates.append(template_name)
Then pass chosen_templates to your template where you will have only
{% for template in chosen_templates %}
{% load template %}
{% endfor %}
Also, I don't quite understand why you are using a dict to select the template with a number that is not in the dictionnary. for key,value in dict.items() may be what you are looking for.
Try without using the block "with"
{% for choice in choice_dict %}
{% if choice =='2' %}
{% include "mod"|add:forloop.counter|add:".html" %}
{% endif %}
{% endfor %}