# django 在网页上显示markdown文本
1 min read
https://blog.csdn.net/riba2534/article/details/84727978
用这个思路稍微改变下,把要显示的content重新定义成md:
def notice(request): notices = models.DHNoticeModel.objects.all().filter(is_show=True).order_by("-notice_sequence") print(notices) qs = [] for i in notices: i.content = markdown.markdown(i.content, extensions=[ 'markdown.extensions.extra', 'markdown.extensions.codehilite', 'markdown.extensions.toc', ]) qs.append(i) context = {"notices": qs} print(context) return render(request, "dh_notice/dh_notice.html", context=context)模板层:
{% extends "dh_notice/base.html" %} {% block dh_notice %} {% for notice in notices %} <div class="news_lines"> <em class="v v02"></em> <span>{{ notice.pub_time | date:"Y/n/d" }}</span>
{% if notice.notice_tag == 1 %} <i class="news_i_1">NEWS</i> {% elif notice.notice_tag == 2 %} <i class="news_i_2">EVENT</i> {% endif %} <b>{{ notice.title }}</b> </div> {# 判断是否是第一次循环 #} {% if forloop.first %} <div class="vcon"> {% else %} <div class="vcon" style="display: none"> {% endif %} <ul class="vconlist clearfix">
{{ notice.content | safe | linebreaksbr }}
</ul> </div> {% endfor %} {% endblock %}