Is there a way to get multiple digits of a given number within a django template?
For example:
{{ some_num|get_digit:2 }}
will give you the second right most digit. For 1224531 it would be 3
Is there a way to get the last 3 digits or the first 5 digits? Like python's slicing?
something like:
{{ some_num|get_digits:2,5}}
There is a the "slice" template tag
https://docs.djangoproject.com/en/dev/ref/templates/builtins/#slice
It uses the same syntax as Python's list slicing.
Example:
{{ some_list|slice:":2" }}
in python this is equivalent to:
some_list[:2]
BTW your 2nd example would be "2:5" not "2,5"
NB. Python slicing works on any 'sequence'. Strings and lists are sequences. Numbers are not!
any extra filter that converts the number into a string before slicing will work. I used these variants:
{{ some_num|slugify|slice:"2:5" }}
and
{{ some_num|stringformat:"d"|slice:"5:10" }}
{{1234567|make_list|slice:'2:5'|join:''}}
Stefano's answer is on the right track. You need a pre-processing step to turn your number into a list, and a post-processing step to merge that list back into string.
You just need to write code as follow for slicing :-
{{valueformoney|slice:"0:4"}}
{% for cloth in valueformoney|slice:"0:4" %}
<div class="product h-100 w-100 border rounded ">
<div class="img__container">
<img src="{{cloth.cloth_image.url}}" alt="" />
</div>
<div class="product__bottom">
<div class="price">
<span class="text-danger"> <del>{% min_price cloth as result %} {{ result|rupee}}</del></span>
<span>{% discount_price cloth as result %}{{result|rupee}}</span>
<span class="float-right badge p-3 badge-info">Save {{cloth.cloth_discount}}% </span>
</div>
<h3 class="p-4">{{cloth.cloth_name}}</h3>
<div class="button">
See More
</div>
</div>
</div>
{% endfor %}
Related
I am trying to concatenate the UUID of a record with a base URL to create a scannable QR code that will link to the direct record on the website. When trying to concatenate the two it fails and yields nothing.
The relevant part is device.id which is a UUID for the device. I've string |stringformat:"s" as well and that didn't work. I don't know what the best practice to do this is and am struggling.
<div class="row">
<div class="col-xs-12 text-center">
{% with "http://127.0.0.1:8000/ims/device/"|add:device.id as deviceurl %}
{% qr_from_text deviceurl size=25 %}
<p class="small text-center">{{deviceurl}}</p>
{% endwith %}
<p class="small text-center">{{ device.id }}</p>
</div>
</div>
Since the |add filter only works with two strings it cannot be used as a general answer. I created a custom |addstr filter and included it in the file which solved the problem.
How to concatenate strings in django templates?
Trying to set variable in django template, and make a simple rule to update it after iteration. Here is my template:
{% for adv in advs %}
<div class="media-item big" style="top: 18%;left:{% cycle '304' '1078' %}px;">
<div class="media-item__tags">
{{ adv.year }}
{{ adv.payer}}
</div>
<div class="media-item__content">
<div class="media-item__background">
<div class="media-item__canvas">
<div class="media-item__canvas-background" style="background-image: url({{adv.image.url}})"></div>
</div>
<h2 class="topic white upcase fixed-size">{{ adv.name }}</h2>
Смотреть проект
</div>
</div>
</div>
In first div i need to make different 'left:' value. I want to make rule: after every iteration, value changes from base=304 to base+774 px. I tryed to do it somehow with {% cycle %} but it doesnt work for me, also tryed to set variables with {% with %} tag, but didnt find any information about how to update them.
You can set the style by multiplying the current counter from 0...n with 774 and add base value 304. For this, you'll need a custom template tag.
Create a templatetags directory in your app. Add an empty __init__.py and multiply_add.py.
multiply_add.py
from django import template
register = template.Library()
#register.simple_tag
def mul_add(a, b, base_value):
return (a * b) + base_value
template.html
{% load multiply_add %}
{% for adv in advs %}
<div class="media-item big" style="top: 18%;left:{% multiply_add forloop.counter0 774 304 %}px;">
<div class="media-item__tags">
{{ adv.year }}
{{ adv.payer}}
</div>
<div class="media-item__content">
<div class="media-item__background">
<div class="media-item__canvas">
<div class="media-item__canvas-background" style="background-image: url({{adv.image.url}})"></div>
</div>
<h2 class="topic white upcase fixed-size">{{ adv.name }}</h2>
Смотреть проект
</div>
</div>
</div>
I'd like to display the plural accordingly in template
{% if page.paginator.count <= 1 %}
<h3 style="display: inline">{{ page.paginator.count }} Comment</h3>
{% else %}
<h3 style="display: inline">{{ page.paginator.count }} Comments</h3>
{% endif %}
Since it's a everyday task,I wonder if it could be achieved in a straightforward way such as template-filer?
Django provides such a function for that. Its name is pluralize
Returns a plural suffix if the value is not 1. By default, this suffix is s.
<h3 style="display: inline"> Comment{{page.paginator.count|pluralize}}</h3>
For words that don’t pluralize by simple suffix, you can specify both a singular and plural suffix, separated by a comma.
countr{{ total|pluralize:"y,ies" }}.
I have the following template code:
<div class="panel panel-default">
<div class="panel-heading">
<h3 class="panel-title">{{ article.title }}</h3>
</div>
<div class="panel-body">
{{ article.content }} <!--will display the full text-->
</div>
I intend to show the first 200 characters of the content, like:
{{ article.content|length=200 }}
How to achieve such a constrain on the text.
There are two template filters here that are useful: slice and truncatechars.
slice limits an iterable (here a string) to a given number, for example:
{{ variable | slice:":200" }}
whereas truncatechars does approximately the same, except that in case the string is longer than the upper bound (here 200), it will slice to the upperbound minus three, and add an ellise:
{{ variable | truncatechars:"200" }}
For a smaller upperbound, to demonstrate the difference, for a string variable = "foobarqux" we would get:
{{ variable | slice:":6" }} # foobar
{{ variable | truncatechars:"6" }} # foo...
The two thus differ: the latter gives a textual hint that there is actually more content. Of course it depends on the specific situation which filter suits your needs.
A nice thing is that you can emulate truncatechars in terms of slice:
{{ variable | truncatechars:":6" }}
is equivalent to:
{% if variable|length > 6 %}{{ variable|slice:":3" }}...{% else %}{{ variable }}{% endif %}
But it is of course not the most elegant solution: in case you want truncatechars behavior, it is better to use the specific filter.
You can use truncatechars filter:
{{ article.content|truncatechars:200 }}
I get the following error code when trying to load the template.
'utf8' codec can't decode byte 0x94 in position 720: invalid start byte
Here is the template:
{% extends "base.html" %}
{% block site_wrapper %}
<div id="main">
Skip to main content
<div id="banner">
<div class="bannerIEPadder">
<div class="cart_box">
[link to cart here]
</div>
Modern Musician
</div>
</div>
<div id="navigation">
<div class="navIEPadder">
[navigation here]
</div>
</div>
<div id="middle">
<div id="sidebar">
<div class="sidebarIEPadder">
[search box here]
<br/>
[category listing here]
</div>
</div>
<div id="content">
<a name=”content”></a>
<div class="contentIEPadder">
{% block content %}{% endblock %}
</div>
</div>
</div>
<div id="footer">
<div class="footerIEPadder">
[footer here]
</div>
</div>
</div>
{% endblock %}
In UTF-8 0x94 is nothing, however in ISO1252 it's a right quote (). Generally speaking the plain quote (") is much safer.
Make sure you're not copying and pasting this out of some blog that has weird accented quotes or something like that.
If you're using a text editor save it as ascii and see what crops up missing.
You have weird double quotes around div#content, try replacing them with ASCII quotes.
Maybe your template is encoded with something other than utf-8? It depends on your terminal/editor or maybe OS settings.
I had some strange characters in my code because i copied out of a pdf-file.
I had this same error . . . and it turned out that the problem was I included a "©" in my source copied as a part of a template.
Got to check that code for strange characters.........