From 564785230c7aabe6edd88a4ce0c762760ee29320 Mon Sep 17 00:00:00 2001 From: crossdark Date: Sat, 28 Sep 2024 12:13:56 +0800 Subject: [PATCH] =?UTF-8?q?markdown=E5=8F=AF=E4=BB=A5=E6=B8=B2=E6=9F=93?= =?UTF-8?q?=E4=BA=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- SinkDark/article/models.py | 8 +++++++- SinkDark/article/signals.py | 6 +++--- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/SinkDark/article/models.py b/SinkDark/article/models.py index f5560e0..db8d511 100644 --- a/SinkDark/article/models.py +++ b/SinkDark/article/models.py @@ -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='

默认内容

') + rendered = RichTextUploadingField(verbose_name='最终内容', default='

默认内容

', 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 diff --git a/SinkDark/article/signals.py b/SinkDark/article/signals.py index ed4d59e..409495f 100644 --- a/SinkDark/article/signals.py +++ b/SinkDark/article/signals.py @@ -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'])"""