我正在使用Django构建一个博客,我只是集成了markdown。如何确保帖子正文的截断部分(告诉您在打开特定帖子时会看到的内容)不会显示降价?
我创建了一个模板过滤器markdown
:
from django.utils.safestring import mark_safe
from django import template
import markdown
register = template.Library()
@register.filter(name='markdown')
def markdown_format(text):
return mark_safe(markdown.markdown(text))
以下是post_list.html
模板中的用法
{% extends "base.html" %}
{% load blog_tags static disqus_tags %}
{% disqus_dev %}
{% block content %}
<div class="jumbotron">
<h1 class="heading-font">Shelter At Your Crossroads</h1>
<p class="lead">...some random ass text that could serve as motto or something</p>
</div>
<div class="container">
<div class="row">
<div class="col-lg-8">
{% if tag %}
<h2 class="mb-4">Posts tagged with "{{ tag.name }}"</h2>
{% endif %}
{% for post in posts %}
<div class="card mb-5">
<img class="card-img-top img-fluid" src="{{ post.image.url }}" alt="{{ post.title }}">
<div class="card-body">
<h2 class="card-title"><a href="{{ post.get_absolute_url }}">{{ post.title }}</a></h2>
<p class="card-text">{{ post.body|markdown|truncatewords_html:50 }}</p>
<a href="{{ post.get_absolute_url }}" class="btn btn-primary"><i class="fa fa-book" aria-hidden="true"></i> Read More </a>
<a href="{% url 'post_share' post.id %}" class="btn btn-outline-secondary"><i class="fa fa-share-alt" aria-hidden="true"></i> Share Post</a>
<div class="float-right">
<i class="fa fa-tags"></i>
{% for tag in post.tags.all %}
<a href="{% url 'post_list_by_tag' tag.slug %}">
<span class="badge badge-pill badge-info">{{ tag.name }}</span>
</a>
{% endfor %}
</div>
</div>
<div class="card-footer text-center text-muted">
Posted on {{ post.publish.date }} by <a href="#">{{ post.author.get_full_name }}</a>
</div>
</div>
{% endfor %}
</div>
<div class="col-lg-4">
<div class="card border-dark mb-3">
<div class="card-header">Post Count</div>
<div class="card-body text-dark">
<h4 class="card-title text-center">Total Number of Posts</h4>
<p class="card-text text-center display-3">{% total_posts %}</p>
</div>
</div>
<div class="card border-dark mb-3">
<div class="card-header">Latest Posts</div>
<div class="card-body text-dark">
<h4 class="card-title text-center">Most Recent Posts</h4>
<p class="card-text">{% show_latest_posts 3 %}</p>
</div>
</div>
<div class="card border-dark mb-3">
<div class="card-header">Latest Comments</div>
<div class="card-body text-dark">
<h4 class="card-title text-center">Most Recent Comments</h4>
<p class="card-text">{% disqus_recent_comments shortname 5 50 1 %}</p>
</div>
</div>
</div>
</div>
{% include 'includes/pagination.html' with page=posts %}
</div>
{% endblock %}
过滤器用于第22行 - {{post.body | markdown | truncatewords_html:50}}
如何只显示普通文字?