Reputation: 27
I'm currently learning Django and I wanted to start off with a really simple blog app. I have a model for my post:
class Post(models.Model):
title = models.CharField(max_length=200)
author = models.ForeignKey(
'auth.User',
on_delete=models.CASCADE,
)
body = models.TextField()
Then I'm referencing it on my home page template:
{% extends 'base.html' %}
{% block content %}
{% for post in object_list %}
<div class="post-entry">
<h2><a href="{% url 'post_detail' post.pk %}">{{post.title}}</a></h2>
<p>{{post.body}}</p>
</div>
{% endfor %}
{% endblock content %}
I would like to make post.body
to be only first 50 or so characters but post.body[:50]
returns syntax error. How could I do that?
Upvotes: 0
Views: 52
Reputation: 1270
Try This:-
You can use
{{ post.body|truncatechars:50 }}
It will display only 50 characters of your Post Body
Upvotes: 1