markdown可以渲染了

This commit is contained in:
跨越晨昏 2024-09-28 12:13:56 +08:00
parent 2ab789be16
commit 564785230c
2 changed files with 10 additions and 4 deletions

View File

@ -3,6 +3,7 @@ from django.utils import timezone
from account.models import MyUser
from ckeditor_uploader.fields import RichTextUploadingField
from mdeditor.fields import MDTextField
import CrossDown # md渲染
class ArticleTag(models.Model):
@ -22,7 +23,7 @@ class ArticleInfo(models.Model):
author = models.ForeignKey(MyUser, on_delete=models.CASCADE, verbose_name='用户')
title = models.CharField('标题', max_length=200)
content = MDTextField(verbose_name='内容')
rendered = RichTextUploadingField(verbose_name='最终内容', default='<p>默认内容</p>')
rendered = RichTextUploadingField(verbose_name='最终内容', default='<p>默认内容</p>', editable=False)
articlephoto = models.ImageField('文章图片', blank=True, upload_to='images/article/')
reading = models.IntegerField('阅读量', default=0)
liking = models.IntegerField('点赞量', default=0)
@ -30,6 +31,11 @@ class ArticleInfo(models.Model):
updated = models.DateTimeField('更新时间', auto_now=True)
article_tag = models.ManyToManyField(ArticleTag, blank=True, verbose_name='文章标签')
def save(self, *args, **kwargs):
# 可以在这里直接更新rendered字段但使用信号更灵活
self.rendered = CrossDown.main(self.content)
super().save(*args, **kwargs)
def __str__(self):
return self.title

View File

@ -10,6 +10,6 @@ def article_signal_handler(sender, instance, created, **kwargs):
# 这里执行你希望在文章创建后自动执行的函数
# 例如:
print("文章已创建,执行特定函数...")
article = ArticleInfo.objects.get(id=instance.id)
print(article.content)
CrossDown.main(article.content)
"""if instance.content:
instance.rendered = instance.content
instance.save(update_fields=['rendered'])"""