I'm trying to pass a value into a template via an include tab, like so:
{% include "shared/page_title.html" with text=local_time_format(job.date, user.timezone, "M j P") %}
So basically, i want the text value to be the result of calling local_time_format (a filter function) with job.date (context object value) and a user property and the final argument.
Getting Could not parse the remainder error-- how to fix?
You can prep values for an include like so:
{% with text=today|date:"D d M Y" %}
{% include "shared/page\_title.html" with text=text%}
{% endwith %}
Related
Is it possible to append 2 context variables together into a single context variable that is calculated at run-time?
For example: if I have {{contextA}} (which equals 1) and {{contextB}} (which equals 'building') and I wanted to add these together to get context {{1building}} How would I do this?
I have tried:
{{contextA + contextB}}
{{{{contextA}} + {{contextB}}}}
{{contextA |add: contextB}}
{{contextA contextB}}
I suppose this is not possible and this needs to be done in the view using python, but it would be ideal if I could just combine context variables in the template.
U can use {% with template tag as follows (docs here -> https://docs.djangoproject.com/en/3.0/ref/templates/builtins/#with)
{% with newvar=contextA|add:contextB %}
{{ newvar }}
{% endwith %}
newvar will have a new value if into a forloop where contextA or contextB change it's value.
As you want to show the value of a context variable with it's name equal to newvar value, the way to accomplish it is to create a custom template tag as follows:
#register.simple_tag(takes_context=True)
def dynvarvalue(context, dynvarname):
""" Returns the value of dynvarname into the context """
return context.get(dynvarname, None)
I did a small proof of concept:
{% with 1building='1 building value' contextA='1' contextB='building' %}
{% dynvarvalue contextA|add:contextB %}
{% endwith %}
Which produces the following output, which I think is what you are asking for:
1 building value
Hope this helps.
Note: take into account that if both variables can be transformed to an integer, they will not be concatenated and will be summed as docs says (https://docs.djangoproject.com/en/3.0/ref/templates/builtins/#add)
Note2: I think there are some security caveats to take into account doing this.
I use the Order Printer app in Shopify to print my orders. I have edited the the template to suit my needs, however I am quiet new to Liquid code.
Based on the shipping postcode of the order, I need the template to return 1 of 3 labels - Rural, Major and Outer. I have a list of postcodes in the following format (this is a small portion for example):
Rural
2648, 2715, 2717-2719, 2731-2739, 3221-3334, 3342-3349, 3351-3352, 3357-3426, 3444-3688, 3691-3749, 3812-3909, 3921-3925, 3945-3974, 3979, 3984-3999
Major
1000-1935, 2000-2079, 2085-2107, 2109-2156, 2158, 2160-2172, 2174-2229, 2232-2249, 2557-2559, 2564-2567, 2740-2744, 2747-2751, 2759-2764, 2766-2774, 2776-2777, 2890-2897
Outer
7020-7049, 7054, 7109-7150, 7155-7171, 7173-7247, 7255-7257, 7330-7799
I'm unable to work out how to use the if statement for the purpose of identifying if the shipping postcode is a rural, major or outer postcode, without typing out every postcode between 7330 and 7799 etc.
Can anyone help?
First declare your arrays:
{% assign Rural= "2648, 2715, 2717-2719, 2731-2739, 3221-3334, ...." | split: ", " %}
{% assign Major= "1000-1935, 2000-2079, 2085-2107, 2109-2156,..." | split: ", " %}
{% assign Outer= "7020-7049, 7054, 7109-7150, 7155-7171,...." | split: ", " %}
Then declare a variable that you will use for the label
{% assign relatedLabel = ""%}
Implement the if logic
{% if Rural contains Order.PosteCode %}
{% assign relatedLabel = "rural" %}
{% endif %}
{% if Major contains Order.PosteCode %}
{% assign relatedLabel = "major" %}
{% endif %}
{% if Outer contains Order.PosteCode %}
{% assign relatedLabel = "outer" %}
{% endif %}
Finally you can print it where you need it
This Poste Code belongs to {{relatedLabel}} area.
In a template, I am using an include like this:
{% include "blah.html" with x="..." only %}
and I can use x in blah.html.
Now, I would like blah.html to set a default value for x if it is not passed by the "parent" template:
{% include "blah.html" only %}
Since there is apparently no builtin template tag to set a variable in a template, I don't know how this could be achieved...
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 %}
I am trying to pass in url parameters to a django template like this...
response = render_to_string('persistConTemplate.html', request.GET)
This the calling line from my views.py file. persistConTemplate.html is the name of my template and request.GET is the dictionary that contains the url parameters.
In the template I try to use one of the parameters like this...
{% for item in (numItems) %}
item {{item}}
{% endfor %}
numItems is one of the url parameters that I am sending in my request like this...
http:/someDomain/persistentConTest.html/?numItems=12
When I try the for loop above, I get an output like this....
image 1 image 2
I am expecting and would like to see the word image printed 12 times...
image 1 image 2 image 3 image 4 image 5 image 6 image 7 image 8 image 9 image 10 image 11 image 12
Can anyone please tell me what I am going wrong?
you can coerce a str to an int using the add filter
{% for item in numItems|add:"0" %}
https://docs.djangoproject.com/en/dev/ref/templates/builtins/#add
to coerce int to str just use slugify
{{ some_int|slugify }}
EDIT: that said, I agree with the others that normally you should do this in the view - use these tricks only when the alternatives are much worse.
I like making a custom filter:
# templatetags/tag_library.py
from django import template
register = template.Library()
#register.filter()
def to_int(value):
return int(value)
Usage:
{% load tag_library %}
{{ value|to_int }}
It is for cases where this cannot be easily done in view.
Yes, the place for this is in the view.
I feel like the above example won't work -- you can't iterate over an integer.
numItems = request.GET.get('numItems')
if numItems:
numItems = range(1, int(numItems)+1)
return direct_to_template(request, "mytemplate.html", {'numItems': numItems})
{% for item in numItems %}
{{ item }}
{% endfor %}
The easiest way to do this is using inbuilt floatformat filter.
For Integer
{{ value|floatformat:"0" }}
For float value with 2 precision
{{ value|floatformat:"2" }}
It will also round to nearest value. for more details, you can check https://docs.djangoproject.com/en/1.10/ref/templates/builtins/#floatformat.
You should add some code to your view to unpack the GET params and convert them to the values you want. Even if numItems were an integer, the syntax you're showing wouldn't give you the output you want.
Try this:
ctx = dict(request.GET)
ctx['numItems'] = int(ctx['numItems'])
response = render_to_string('persistConTemplate.html', ctx)
In my case one of the items was a string and you can not compare a string to an integer so I had to coerce the string into an integer see below
{% if questions.correct_answer|add:"0" == answers.id %}
<span>Correct</span>
{% endif %}
You can do like that: if "select" tag used.
{% if i.0|stringformat:'s' == request.GET.status %} selected {% endif %}
My solution is kind of a hack and very specific..
In the template I want to compare a percentage with 0.9, and it never reaches 1, but all the values are considered string in the template, and no way to convert string to float.
So I did this:
{% if "0.9" in value %}
...
{% else %}
...
{% endif %}
If I want to detect some value is beyond 0.8, I must do:
{% if ("0.9" in value) or ("0.8" in value) %}
...
{% else %}
...
{% endif %}
This is a hack, but suffice in my case. I hope it could help others.