22👍
✅
You could use slice for pre-django 1.4:
{% if place or other_place or place_description%}
{% with place|add:other_place|add:place_description as pl %}
{% if pl|length > 80 %}
{{pl|slice:80}}...
{% else %}
{{pl }}
{% endif %}
{% endwith %}
{% endif %}
If you are using django 1.4 or greater,
You can just use truncatechars
{% if place or other_place or place_description%}
{% with place|add:other_place|add:place_description as pl %}
{{pl|truncatechars:80}}
{% endwith %}
{% endif %}
4👍
You could probably do it with a combination of add/truncatechars e.g.
{{ place|add:other_place|add:place_description|truncatechars:80}}
- How to use a localized "short format" when formatting dates in Django?
- Using arbitrary methods or attributes as fields on Django ModelAdmin objects?
- Django Multiple Databases Fallback to Master if Slave is down
3👍
You could also use ‘cut’ which is part of django template builtins
for example if
{{ file.pdf.name}}
gives ‘store/pdfs/verma2010.pdf’
{{ file.pdf.name | cut:'store/pdfs/'}}
Would give ‘verma2010.pdf’
- Django-signals vs triggers?
- Django binary (no source code) deployment
- How do I get AWS credentials in the AWS ECS docker container?
- How to filter for multiple ids from a query param on a GET request with django rest framework?
- Django – null value in column violates not-null constraint in Django Admin
1👍
Took me way too long to find the answer in 2022. It’s truncatechars
, e.g.
{{ my_string|truncatechars:1 }}
https://docs.djangoproject.com/en/3.2/ref/templates/builtins/#truncatechars
- Passing Pk or Slug to Generic DetailView in Django?
- When I use Django Celery apply_async with eta, it does the job immediately
Source:stackexchange.com