gpt4 book ai didi

python - Django 帖子中的 "read more"

转载 作者:太空狗 更新时间:2023-10-29 16:50:49 25 4
gpt4 key购买 nike

我正在 django/webfaction 上创建一个博客。目前我的主页显示所有帖子的所有内容。我想调整它以仅显示每篇文章的几行,并且每篇文章都以“阅读更多”链接结尾。如何做到这一点?我是 django 和 python 的新手。请帮助我。

home.html 中的代码:

{% block content %}

{% for post in object_list %}
<h2>{{ post.title }} </h2>

<div class = "post_meta">
on {{ post.created}}
</div>

<div class = "post_body">
{{ post.body|safe|linebreaks}}
</div>

{% endfor %}

{% endblock %}

提前致谢。

最佳答案

您可以使用内置模板过滤器截断 doc

  <div class = "post_body">
{{ post.body|safe|truncatewords:"50"|linebreaks }}
<a href="{{ url_for_full_content }}">read more</a>
</div>

关于python - Django 帖子中的 "read more",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12775565/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com