8๐
โ
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>
3๐
You can override save() method
def save(self, *args, **kwargs):
if len(self.product_name) > 20:
self.product_name = self.product_name[:20] + '...'
super().save(*args, **kwargs)
๐คweAreStarsDust
1๐
You can get the length of a string using len
You can cut a string up using slices [:]
You can add the ellipsis to a string if the length is longer than a certain threshold.
if len(product_name) > 15:
product_name = product_name[:15] + "..."
๐คAlgoRythm
- [Django]-Django Hierarchy Permissions
- [Django]-Allowing both email and username login in Django project
- [Django]-Understanding python imports
- [Django]-How can I add javascript file to my ModelForm in django?
- [Django]-Explicitly clear django memcached flush/cache() OR delete specific per-view-cache key
Source:stackexchange.com