From 2ab789be167ade000bfa643503eb8b4f49a642e8 Mon Sep 17 00:00:00 2001 From: crossdark Date: Fri, 27 Sep 2024 23:00:31 +0800 Subject: [PATCH] =?UTF-8?q?singles=E5=BF=AB=E5=A5=BD=E4=BA=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- SinkDark/SinkDark/settings.py | 17 ++++++++--------- SinkDark/article/admin.py | 2 ++ SinkDark/article/apps.py | 4 ++++ SinkDark/article/models.py | 2 +- SinkDark/article/signals.py | 15 +++++++++++++++ 5 files changed, 30 insertions(+), 10 deletions(-) create mode 100644 SinkDark/article/signals.py diff --git a/SinkDark/SinkDark/settings.py b/SinkDark/SinkDark/settings.py index 4e855d2..cb8dc63 100644 --- a/SinkDark/SinkDark/settings.py +++ b/SinkDark/SinkDark/settings.py @@ -3,7 +3,6 @@ from pathlib import Path # Build paths inside the project like this: BASE_DIR / 'subdir'. BASE_DIR = Path(__file__).resolve().parent.parent - # SECURITY WARNING: keep the secret key used in production secret! SECRET_KEY = '+f%vbh_8b+(*kp=clju)691popu%a*#60ik99n4(n2ex9g$)_a' @@ -63,8 +62,7 @@ TEMPLATES = [ WSGI_APPLICATION = 'SinkDark.wsgi.application' - -DATABASES = { +"""DATABASES = { 'default': { 'ENGINE': 'django.db.backends.mysql', 'NAME': 'test', @@ -72,11 +70,17 @@ DATABASES = { 'PASSWORD': 'Clever-3366', 'HOST': 'crossdark.net', 'PORT': '3306', + }, +}""" + +DATABASES = { + 'default': { + 'ENGINE': 'django.db.backends.sqlite3', + 'NAME': BASE_DIR / 'db.sqlite3', } } - AUTH_PASSWORD_VALIDATORS = [ { 'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator', @@ -92,8 +96,6 @@ AUTH_PASSWORD_VALIDATORS = [ }, ] - - LANGUAGE_CODE = 'en-us' TIME_ZONE = 'UTC' @@ -102,14 +104,11 @@ USE_I18N = True USE_TZ = True - DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField' # 配置自定义用户模型MyUser AUTH_USER_MODEL = 'account.MyUser' - - STATIC_URL = '/static/' STATICFILES_DIRS = [BASE_DIR / 'publicStatic'] STATIC_ROOT = BASE_DIR / 'static' diff --git a/SinkDark/article/admin.py b/SinkDark/article/admin.py index baa99e2..efb3188 100644 --- a/SinkDark/article/admin.py +++ b/SinkDark/article/admin.py @@ -4,6 +4,8 @@ from .models import * admin.site.site_title = '博客管理后台' admin.site.site_header = '博客管理' +# admin.site.register(ArticleInfo) # 将文章模型注册到admin后台的mdeditor + @admin.register(ArticleTag) class ArticleTagAdmin(admin.ModelAdmin): diff --git a/SinkDark/article/apps.py b/SinkDark/article/apps.py index 2bc48aa..cb9626d 100644 --- a/SinkDark/article/apps.py +++ b/SinkDark/article/apps.py @@ -4,3 +4,7 @@ from django.apps import AppConfig class ArticleConfig(AppConfig): default_auto_field = 'django.db.models.BigAutoField' name = 'article' + + def ready(self): + from . import signals # 导入signals模块以注册信号处理器 + diff --git a/SinkDark/article/models.py b/SinkDark/article/models.py index 1af27ca..f5560e0 100644 --- a/SinkDark/article/models.py +++ b/SinkDark/article/models.py @@ -22,7 +22,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 = MDTextField(verbose_name='最终内容', default='

默认内容

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

默认内容

') articlephoto = models.ImageField('文章图片', blank=True, upload_to='images/article/') reading = models.IntegerField('阅读量', default=0) liking = models.IntegerField('点赞量', default=0) diff --git a/SinkDark/article/signals.py b/SinkDark/article/signals.py new file mode 100644 index 0000000..ed4d59e --- /dev/null +++ b/SinkDark/article/signals.py @@ -0,0 +1,15 @@ +from django.db.models.signals import post_save +from django.dispatch import receiver +from .models import ArticleInfo +import CrossDown + + +@receiver(post_save, sender=ArticleInfo) +def article_signal_handler(sender, instance, created, **kwargs): + if created: + # 这里执行你希望在文章创建后自动执行的函数 + # 例如: + print("文章已创建,执行特定函数...") + article = ArticleInfo.objects.get(id=instance.id) + print(article.content) + CrossDown.main(article.content)