效果如下:

views.py
try:
thisarticle = get_object_or_404(Articles, id=article_id, status='有效')
except:
return render(request,'404.html')
md = markdown.Markdown(extensions=[
'markdown.extensions.extra',
'markdown.extensions.codehilite',
'markdown.extensions.toc',
# TocExtension(slugify=slugify)
]
)
thisarticle.body = md.convert(thisarticle.body)
context = {
'toc':md.toc
}
return render(request, 'single.html', context=context) # 返回info.html页面
核心部分就这些
html
<div class="widget" data-wow-delay="0.4s">
<header class="widget-head">
<h3>This Catagory</h3>
</header>
<div style="font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif">
{% block toc %}
{{ toc|safe }}
{% endblock toc %}
</div>
</div>
本文介绍了一种使用Python的Django框架处理特定ID的文章,通过get_object_or_404方法获取文章实例,利用markdown库转换文章正文并生成目录(TOC),最后将转换后的正文和目录传递给模板进行渲染。
1800

被折叠的 条评论
为什么被折叠?



