Display singular and plural headers on appropriate context - django

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" }}.

Related

Replace function inside value not working

Maybe I'm doing it wrong (tried several ways to achieve my goal) or overlooking something here.
What I'd like to achieve is this:
When I use the Live-search function, I get categories containing the search keyword (like: Paint -> Paint buckets, Paint brushes, Paint colors etc.) which works like a charm. The one thing I need is to style the searched keyword in the presented categories like:
Paint bucket,
Paint brushes,
Color paint
This is the code I have at the moment:
{% if (products.length + categories.length + pages.length) == 0 %}
<div id="Pi56dYpB" class="undefined"> No results for:
<b>{{ query }}</b>...
</div>
{% endif %}
{% if categories.length > 0 %}
<div class="categories">
{% for category in categories %}
{% if loop.index < 7 %}
<a class="p-0" href="{{ category.url }}" style="all:inherit;">
<h3 id="bvwiyjEN" class="undefined">{{ category.name|replace({{ query }}: "<strong>"{{ query }}"<strong>"})|raw }}</h3>
</a>
{% endif %}
{% endfor %}
{% endif %}
Unfortunately this isn't working. I did check if the {{ query }} value is accessible with this simple line of code:
<h3 id="bvwiyjEN" class="undefined">{{ category.name }} - {{ query }}</h3>
No problems found here.
Did I use the wrong syntax in my code maybe? Any help would be appreciated!
replace({{ query }}) is the wrong syntax - replace(query) should work, as you don't want to echo the variable query here
As Nico Haase pointed out already, {{ ... }} is used to output variables, this translate to something like <?php echo $foo; ?> and can't/shouldn't be used inside statements
Furthermore, the filter replace expects an array of keys to replace with the values.
You would need to change your code to the following:
{% for category in categories %}
{{ category|replace({ (query): '<strong>'~query~'</strong>',})|raw }}
{% endfor %}
demo
Some notes:
The filter replace uses the PHP function strtr in the background. This mean the output will be case-sensitive. If you don't want this, I'd advice you to follow through here
Wrapping the key with parantheses is mandatory. This will force twig to evaluate/interpolate the value of the key - demo

Concatenate string and UUID in Django Template

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?

Display part of the content data

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 }}

Can I specify a multiline context variable/parameter when {% include %}ing a template?

I know that it is possible to set context variables when including a Django template from another template using
{% include "default_table.html" with table_header=table_header1 table_data=table_data1 %}
or
{% with "My data" as table_data %}
{% include 'default_table.html' %}
{% endwith %}
My issue with this is that both approaches don't let me define multiline variables (unless they are based on a previous multiline variable of course).
My specific usecase is this
<!-- widget.html -->
<div class="box">
<div class="title">{{ title }}</div>
<div class="title">{{ body }}</div>
</div>
and I'd like to be able to set a longer text for the body context variable. This will make is possible for me to reuse common widget HTML in various places. Can this be done?
I've been searching a bit on http://djangosnippets.org for an über {% with ... %} template tag, but haven't found any so far.
This Django snippet kinda solves my issue: http://djangosnippets.org/snippets/1860/ But I'd love to be able to set context variables instead of defining {% localblock step_ready_js %}{% endlocalblock %} in my widget HTML.

django slice numbers in template

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 %}