如何在djang模板主页面显示短版post

2024-05-16 21:50:04 发布

您现在位置:Python中文网/ 问答频道 /正文

我写了一个博客应用程序,我想显示在主页上的帖子短版本,标题后,然后将只有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', {})

输出屏幕截图:

enter image description here


Tags: 版本应用程序returnrequestdefhtmlcontext文章
1条回答
网友
1楼 · 发布于 2024-05-16 21:50:04

可能您可以在模板中使用^{}^{}。像这样:

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

相关问题 更多 >