Django个人博客搭建教程---自动生成Markdown文章目录

it2025-08-23  16

效果如下:

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>

 

最新回复(0)