首页 > 解决方案 > 如何在 django 模板的主页中显示简短版本的帖子

问题描述

我正在写一个博客应用程序,我想在主页上显示帖子的简短版本,在标题之后,每个帖子只有 4 行详细信息,然后有另一个帖子,但问题是所有显示在主页面中的详细信息页。

def tours(request):
    featured = Tour.objects.filter(featured=True)

    context = {
        "object_list": featured,
    }

    return render(request, 'tours.html', context)

def sidebar(request):
    return render(request, 'sidebar.html', {})

输出截图:

在此处输入图像描述

标签: pythondjango

解决方案


可能您可以在模板中使用truncatecharsor trancatewords。像这样:

{% for post in object_list %}
    {{ post.detail|truncatechars:100 }} // assuming post.detail is the field where you write the body of the tours.
{% endfor %}

推荐阅读