Django如何渲染markdown

2020-06-12 09:04:20 浏览数 (2)

依赖包

代码语言:javascript复制
pip install markdown django-markup bleach bleach-whitelist

示例代码

  • your_app/urls.py
代码语言:javascript复制
urlpatterns = [
    path('about/', views.about, name="about"),
]
  • your_app/views.py
代码语言:javascript复制
def about(request):
    content = ""
    with open("./templates/license.md", 'r') as f:
        content = formatter(f.read(), filter_name="markdown")
    return render(request, 'about.html', {"content":content})
  • templates/about.html
代码语言:javascript复制
    {% load markup_tags %}
    {{ content|apply_markup:"markdown" }}

0 人点赞