72๐
{{ value|slice:"5" }}{% if value|length > 5 %}...{% endif %}
Update
Since version 1.4, Django have a built-in template tag for this:
{{ value|truncatechars:9 }}
- [Django]-Django unit tests without a db
- [Django]-Django admin ManyToMany inline "has no ForeignKey to" error
- [Django]-Django Rest Framework โ no module named rest_framework
12๐
I made my own template filter, that add โโฆโ to the end of (last word of) the (truncated) string as well:
from django import template
register = template.Library()
@register.filter("truncate_chars")
def truncate_chars(value, max_length):
if len(value) > max_length:
truncd_val = value[:max_length]
if not len(value) == max_length+1 and value[max_length+1] != " ":
truncd_val = truncd_val[:truncd_val.rfind(" ")]
return truncd_val + "..."
return value
- [Django]-How to run own daemon processes with Django?
- [Django]-How to check if a user is logged in (how to properly use user.is_authenticated)?
- [Django]-Django models: default value for column
11๐
If you go on creating your own custom template tag, consider to leverage the native Django util Truncator.
The following is a sample usage of the Truncator util:
>>> from django.utils.text import Truncator
>>> Truncator("Django template tag to truncate text")
<Truncator: <function <lambda> at 0x10ff81b18>>
>>>Truncator("Django template tag to truncate text").words(3)
u'Django template tag...'
Truncator("Django template tag to truncate text").chars(20)
u'Django template t...'
And here how you can use it inside a Django template tag:
from django import template
from django.utils.text import Truncator
register = template.Library()
@register.filter("custom_truncator")
def custom_truncator(value, max_len, trunc_words=False):
""" Set trunc_words=True to truncate by words instead of by chars."""
truncator = Truncator(value)
return truncator.words(max_len) if trunc_words else truncator.chars(max_len)
Eventually you can import the custom_truncator in any Django template.
- [Django]-Django: Grab a set of objects from ID list (and sort by timestamp)
- [Django]-Best practices for getting the most testing coverage with Django/Python?
- [Django]-Django manage.py runserver invalid syntax
4๐
Here it is in the Django Documentation, Built-in template tags and filters: truncatechars
- [Django]-Testing email sending in Django
- [Django]-Get the list of checkbox post in django views
- [Django]-Django Rest Framework โ Updating a foreign key
3๐
You should write a custom template filter: http://docs.djangoproject.com/en/dev/howto/custom-template-tags/#writing-custom-template-filters
Have a look at how truncatewords
is built in django.utils.text
- [Django]-Why does DEBUG=False setting make my django Static Files Access fail?
- [Django]-AccessDenied when calling the CreateMultipartUpload operation in Django using django-storages and boto3
- [Django]-Pylint "unresolved import" error in Visual Studio Code
3๐
You can achieve your goal with similar code:
{{ value_of_text|truncatechars:NUM_OF_CHARS_TO_TRUNCATE}}
where NUM_OF_CHARS_TO_TRUNCATE
is number of chars to leave.
- [Django]-Django DoesNotExist
- [Django]-Django connection to postgres by docker-compose
- [Django]-How to disable admin-style browsable interface of django-rest-framework?
- [Django]-Why is logged_out.html not overriding in django registration?
- [Django]-Django: Example of generic relations using the contenttypes framework?
- [Django]-Django dump data for a single model?
0๐
Adding a โtruncateโ filter was a feature request for 4 years but finally landed in trunk, as far as I understand https://code.djangoproject.com/ticket/5025 โ so weโve to wait for the next release or use trunk.
- [Django]-How to access the local Django webserver from outside world
- [Django]-Parsing unicode input using python json.loads
- [Django]-Django aggregate or annotate