django3.1 后台添加markdown支持
首先安装 django-mdeditor
python pip install django-mdeditor
settings.py 中添加
1 | INSTALLED_APPS = [ |
添加站点路由 mysite/urls.py
1 | from django.contrib import admin |
修改blog下models.py
中内容类型
导入MDTextField
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20from django.db import models
from mdeditor.fields import MDTextField
# Create your models here.
class BlogPost(models.Model):
title = models.CharField(max_length= 150)
# body = models.TextField()
body = MDTextField()
create_time = models.DateTimeField()
def __str__(self):
return f"<BlogPost {self.title}>"
class Meta:
verbose_name ="文章"
verbose_name_plural = verbose_name
blog应用views.py
1 |
|
前台页面添加markdown css支持
1 | <link rel="stylesheet" href="https://cdn.bootcss.com/github-markdown-css/3.0.1/github-markdown.css"> |
前台页面添加 safe渲染
1 | {{ blog.title }} |
重新启动服务
python manage.py runserver 127.0.0.1:8000