Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I limit a text length and add dots to show continuation python django

Tags:

python

django

Am working on an Ecommerce website using django. My problem is that some products name are longer that the other, which makes the product display look odd.

Question: How do I limit the length of product name and add dots to show that it continue

Example: product_name = "Men Suede Loafers Moccasins Shoes -Black"

will look like product_name = "Men Suede Loafers Mocca...."

like image 971
Bruno Avatar asked Oct 27 '25 10:10

Bruno


1 Answers

You can make use of the |truncatechars template filter [Django-doc]:

{{ product_name|truncatechars:23 }}

While you thus store the product_name as full text, it is rendered for the first 23 characters, and then followed by an ellipsis (…).

You can also add a tooltip that shows the text in full, for example:

<p title="{{ product_name }}">{{ product_name|truncatechars:23 }}</p>
like image 66
Willem Van Onsem Avatar answered Oct 30 '25 00:10

Willem Van Onsem



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!