文章目录[隐藏]
Markdown用着有多爽我就不说了,最近学习Django就也尝试搞了一下。
Markdown 配置
pip install Markdown
Django 配置
- models.py
class MarkDownText(models.Model):
obj = models.Manager()
content = models.TextField()
- 模板配置,注意其中的safe标签转码就行,不转码的话建议找个js框架去前端转码。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<div align="center">
{{content|safe}}
</div>
</body>
</html>
- views.py配置
from .models import MarkDownText
import markdown as md
def ShowMarkDown(request):
try:
content = MarkDownText.obj.first().content
content = md.markdown(content,extensions=['markdown.extensions.fenced_code'])
return render(request,"myApp/showmarkdown.html",{"content":content})
except Exception as e:
return render(request,"myApp/showmarkdown.html",{"content":e})
- urls.py路由配置
url(r'^markdown/$',views.ShowMarkDown,name="markdown")