Django: Adding Markdown Support for Template Rendering
Two methods Django's django-markdown-deux module Python module markdown django-markdown-deux First, you need to install: pip install django-markdown-deux Modify setting.py Add markdown-deux to it INSTALLED_APPS = [ 'markdown-deux', ] Add tags in the template
目录3 个章节
Two methods
- Django's
django-markdown-deuxmodule - Python module
markdown
django-markdown-deux
First, you need to install:
pip install django-markdown-deux
Modify setting.py
Add markdown-deux to it
INSTALLED_APPS = [
'markdown-deux',
]
Add tags in the template
After loading the markdown-deux-tags tag, add a filter to the markdown content and it can be parsed into html.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Doc - {{ docname }}</title>
<link href="/static/css/highlights/github.css"/>
</head>
<body>
{ % load markdown-deux-tags % }
{{ content | markdown }}
</body>
</html>
Rendering with the markdown module
First, install
pip install markdown
Use markdown rendering in views.py
import markdown
def doc(request, name):
template = get_template('doc.html')
docfile = get_template('doc/{}.md'.format(name))
content = docfile.render()
html = template.render({
'docname': name,
'content':
markdown.markdown(content,
extensions=[
'markdown.extensions.extra',
'markdown.extensions.codehilite',
'markdown.extensions.toc',
])
})
return HttpResponse(html)
The markdown module will directly render markdown-formatted documents into html format. However, Django's templates will escape html, so you still need to modify the template a bit.
Modify doc.html
Add the safe filter to content, indicating that escaping is not needed and the original content should be displayed directly.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Doc - {{ docname }}</title>
<link href="/static/css/highlights/github.css"/>
</head>
<body>
{{ content | safe }}
</body>
</html>
_EOF_
Django: Adding Markdown Support for Template Rendering
- 本文作者:
- 程序设计实验室
- 版权声明:
- 本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!
相关文章
Django中「强行」使用MVVM设计模式
扫盲 首先带大家了解一下什么是MVVM模式: 什么是MVVM?MVVM是Model-View-ViewModel的缩写。 MVVM是MVC的增强版,实质上和MVC没有本质区别,只是代码的位置变动而已 从名字上看,MVVM比MVC架构中多了一个ViewModel,没错,就是这个ViewModel,他是MVVM相对于MVC改进的核心思想。在开发过程中,由于需求的变更或添加,项目的复杂度越来越高,代码量
2021-12-18 · Django · 约 1 分钟
Django统一包装接口返回数据格式
前言 最近实在太忙了,开始了一个新的项目,为了快速形成产品,我选择了Django来实现后端,然后又拿起了之前我封装了项目脚手架「DjangoStarter」。 由于前段时间我写了不少.NetCore的后端代码,对CRUD的接口开发又有了一些新的理解,所以肯定也要把DjangoStarter改造一番,改得更加顺手~ 题外话:话说我在前端终于真正用上react,不得不说 ts + react 真好用,
2022-08-26 · Django · 约 1 分钟
Django+Logging:日志记录配置与使用方法
前言 日志在程序开发中是少不了的,通过日志我们可以分析到错误在什么地方,有什么异常。在生产环境下有很大的用途。在Java开发中通常用log4j,logback等第三方组件。那么在django中是怎么处理日志?django利用的就是Python提供的logging模块,但django中要用logging,还得有一定的配置规则,需要在setting中设置。 Logging模块 Logging模块为应用
2022-04-02 · Django · 约 1 分钟
Django过时了吗?从ASGI到AI时代的思考
Django过时?技术趋势变了而已,异步支持短板在AI时代更明显。生态工程化再稳,也挡不住轻量框架和ASGI的冲击。独立开发者该考虑新选择了?
2025-09-12 · Django · 约 1 分钟
讨论
共 0 条加载评论中…