Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I cut out the string in the Django template?

Tags:

python

django

This is my code in templates:

{% for wine_con in data.winry_consult %}
    <li><a href="/article_list_content-{{win_con.id}}/"><p>{{ win_con.content }}</p><i class="font">&#xe6aa;</i></a></li>
{% endfor %}

I found the win_con.content is too long for my website, I only want 5 count characters, how can I cut out the win_con.content in template?

like image 415
user7693832 Avatar asked Aug 15 '17 12:08

user7693832


People also ask

What is truncate in Django?

Django-Truncate is a simple library that will add the ability to empty any given model in any app within your django project, in a more SQL related words it will TRUNCATE the TABLE with a simple command: python manage.py truncate --apps myapp --models model1 model2.

How do I override a Django template?

To override these templates, you will need to have an admin folder in your templates folder. If you do not have a templates folder, you can create it in the main project folder. To do so, you will have to change the project's settings.py . Find the TEMPLATES section and modify accordingly.

What does Django template contains?

Being a web framework, Django needs a convenient way to generate HTML dynamically. The most common approach relies on templates. A template contains the static parts of the desired HTML output as well as some special syntax describing how dynamic content will be inserted.


2 Answers

you may use built-in filter truncatechars

{{ win_con.content|truncatechars:5 }}
like image 51
Brown Bear Avatar answered Oct 27 '22 09:10

Brown Bear


https://docs.djangoproject.com/en/1.11/ref/templates/builtins/#truncatechars should help

{{ value|truncatechars:9 }}
like image 24
pkisztelinski Avatar answered Oct 27 '22 11:10

pkisztelinski