How to use "for" statement in Django template - django

Here is Django template code.
I tried to put out images to template.
amount of image put out to template is depending on {{object.image_amount}}
However since {{object.image_amount}} is an integer I can not iterate like this
{% for i in object.image_amount %}
So I tried this but does not work as well.
{% for i in range object.image_amount %}
What should I do?

if you already have images that you need to show in your template
something like object.images or object.images.all if its related model
you can do
{% for image in object.images.all %}

Related

Accessing view that rendered the template from the template in django

I was wondering if it is possible to determine which view rendered a specific template in Django (or get a boolean result). For example:
{% if this_view %}
some code
{% else %}
some other code
{% endif %}
I'm probably way off but I was wondering how we could create similar conditional statement within the template. Any help would be much appreciated!
You can use the django's request.get_full_path to get the current url in the template and from the url you should be able to figure out the view.
You can also use request.resolver_match.view_name in your template to get the view name

what is difference between {{}} and {% %} in django templates

I am very new to django and working on it.. I visited a html file and dont know the difference between {{}} and {% %} in html files used
as here
{% load static %}
Thanks a lot
You can use
{% %} For sentences such as if and for or to call tags such as load, static, etc.
{{ }} To render variables in the template.
Read More about it at Django Docs
{% %} is for displaying code and {{}} is for displaying variables
There are three things in the template in Django
First is template variable and the second thing is template tag and third and last is template filter
so we write a template variable is {{}}
and write a template tag is {% %}
third and last is template filter {{variable |filter:arg}}
I'm new too for Django, so if i'm wrong, please someone correct me.
The difference between they are:
{{variable}} is used to use a variables. When the template encounters a variable, it evaluates that variable and replaces it with the result.
You also can use filters {{variable|filter}} like this:
{{name|length}} in this case you will use a variable "name" and return the length of that variable.
{%tag%} could use for loops or logic, or load external information into the template to be used by later variables. You can create block tags to help extend other html files parts. Also you can create custom tags.
A good place to see how to do it:
https://www.codementor.io/hiteshgarg14/creating-custom-template-tags-in-django-application-58wvmqm5f
Tags like loops and block, need to be closed.
{% %} for IF ELSE CONDITIONS and FOR LOOP etc
{{ }} for veriables that rendered from view function also used in FOR LOOP veriables like
`enter code here`
{% for obj in qs%}
{{ obj.veriable_name }}
{% endfor %}

Django template custom tag as boolean

This seems such a simple thing but I can't seem to get it to work for one, and two I can't seem to be getting a straight forward answer online on wether it can or can't be done.
I just want a simple tag that will work like so
{% if my_tag %}
render something
{% else %}
render something else
{% endif %}
now I don't care about filters or any other ways you might be able to do the same thing, I want it to look exactly like that, and work like described, I have a simple tag made that actually does return True or False as needed, and it's called if I call the tag like this
{% my_tag %}
however it does't get called if I add an if in front of the tag, is a feature this simple and logical not implemented?
Like the link to the potential duplicate states, you can use an assignment tag similar to the following that will return the current time:
#register.assignment_tag
def get_current_time(format_string):
return datetime.datetime.now().strftime(format_string)
Within your template you can then do what you are desiring to do:
{% if get_current_time %}
...show time
{% else %}
...don't show time
{% endif %}

Django Pass Multiple Parameters to Custom Template Filter Inside If Statement

I have an issue. I've written a custom template tag with a function signature like this-
def has_paid_for_article(article, request):
Now, in my template tag I have a conditional statement to determine whether a user can download an article or not (this is determined by if the article is older than two years or the logged in user has paid for the article). Here's the snippet-
{% if article|is_older_than_two_years %}
<span class="amp">& </span>{% get_article_download_link article %}
{% else %}
download
{% endif %}
The aforementioned snippet works fine, however I need to call the has_paid_for_article() function inside of a conditional statement. I've tried the following ways to make this happen-
{% if article|is_older_than_two_years or article|request|has_paid_for_article %}
,
{% if article|is_older_than_two_years or [article, request]|has_paid_for_article %}
This one works outside of the conditional statement-
{% if article|is_older_than_two_years or has_paid_for_article article request %}
What would be the correct syntax here? Also, I've read other posts on the topic, I CANNOT put this logic in the view. I won't go into detail, but with the way it works, that is not an option. Thank you!
Try
{% if article|is_older_than_two_years or article|has_paid_for_article:request %}
See Writing custom template filters

How do I create a template tag for including template files without parsing the template tags in Django?

Im using Mustache.js to fill dynamically the html content, i know that I can use the {% templatetag %} tag, but I want to use these files as Django templates and as Mustach.js templates, and also I found te template tag too large for programming.
I want to create a new tag like {% include_template "templates/none/absolute/url.hmtl" %} or is it possible to extend the {% include %} tag to receive a "no_parse" param? it would be perfect to use it as:
{% include "my_template.html" no_parse %}
Where my_template.html looks something like:
Hello {{user}}, you have {{points}} points!
The built-in ssi template tag should do what you want: https://docs.djangoproject.com/en/1.3/ref/templates/builtins/#ssi
{% ssi "my_template.html" %}
Make sure to read the note on defining ALLOWED_INCLUDE_ROOTS.