diff --git a/.gitignore b/.gitignore
index 3996e81..1a478c6 100644
--- a/.gitignore
+++ b/.gitignore
@@ -174,3 +174,6 @@ cython_debug/
# PyPI configuration file
.pypirc
+# 忽略媒体上传目录
+*/media/*
+
diff --git a/.idea/.gitignore b/.idea/.gitignore
new file mode 100644
index 0000000..e741503
--- /dev/null
+++ b/.idea/.gitignore
@@ -0,0 +1,8 @@
+# 默认忽略的文件
+/shelf/
+/workspace.xml
+# 基于编辑器的 HTTP 客户端请求
+/httpRequests/
+# Datasource local storage ignored files
+/dataSources/
+/dataSources.local.xml
diff --git a/.idea/blog.iml b/.idea/blog.iml
new file mode 100644
index 0000000..e339442
--- /dev/null
+++ b/.idea/blog.iml
@@ -0,0 +1,30 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/.idea/inspectionProfiles/profiles_settings.xml b/.idea/inspectionProfiles/profiles_settings.xml
new file mode 100644
index 0000000..449e696
--- /dev/null
+++ b/.idea/inspectionProfiles/profiles_settings.xml
@@ -0,0 +1,6 @@
+
+
+
+
+
+
\ No newline at end of file
diff --git a/.idea/misc.xml b/.idea/misc.xml
new file mode 100644
index 0000000..898f0c0
--- /dev/null
+++ b/.idea/misc.xml
@@ -0,0 +1,7 @@
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/.idea/modules.xml b/.idea/modules.xml
new file mode 100644
index 0000000..31bd981
--- /dev/null
+++ b/.idea/modules.xml
@@ -0,0 +1,8 @@
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/.idea/vcs.xml b/.idea/vcs.xml
new file mode 100644
index 0000000..0faa797
--- /dev/null
+++ b/.idea/vcs.xml
@@ -0,0 +1,6 @@
+
+
+
+
+
+
\ No newline at end of file
diff --git a/myblog/blog/admin.py b/myblog/blog/admin.py
index b0ca294..6bf1c84 100644
--- a/myblog/blog/admin.py
+++ b/myblog/blog/admin.py
@@ -1,17 +1,17 @@
from django.contrib import admin
from .models import Post
from django.db import models
-from martor.widgets import AdminMartorWidget
+from mdeditor.widgets import MDEditorWidget
class PostAdmin(admin.ModelAdmin):
- # 使用Martor Markdown编辑器替换默认的Textarea
+ # 使用MDEditor Markdown编辑器替换默认的Textarea
formfield_overrides = {
- models.TextField: {'widget': AdminMartorWidget},
+ models.TextField: {'widget': MDEditorWidget},
}
# 设置列表显示字段
- list_display = ('title', 'publish_date', 'created_at')
+ list_display = ('title', 'publish_date', 'created_at', 'updated_at')
# 设置搜索字段
search_fields = ('title', 'content')
diff --git a/myblog/blog/migrations/0003_post_image.py b/myblog/blog/migrations/0003_post_image.py
new file mode 100644
index 0000000..f1ca44d
--- /dev/null
+++ b/myblog/blog/migrations/0003_post_image.py
@@ -0,0 +1,18 @@
+# Generated by Django 5.2.4 on 2025-07-26 13:33
+
+from django.db import migrations, models
+
+
+class Migration(migrations.Migration):
+
+ dependencies = [
+ ('blog', '0002_post_publish_date'),
+ ]
+
+ operations = [
+ migrations.AddField(
+ model_name='post',
+ name='image',
+ field=models.ImageField(blank=True, null=True, upload_to='post_images/'),
+ ),
+ ]
diff --git a/myblog/blog/migrations/0004_alter_post_content.py b/myblog/blog/migrations/0004_alter_post_content.py
new file mode 100644
index 0000000..4948cd3
--- /dev/null
+++ b/myblog/blog/migrations/0004_alter_post_content.py
@@ -0,0 +1,19 @@
+# Generated by Django 5.2.4 on 2025-07-26 15:26
+
+import mdeditor.fields
+from django.db import migrations
+
+
+class Migration(migrations.Migration):
+
+ dependencies = [
+ ('blog', '0003_post_image'),
+ ]
+
+ operations = [
+ migrations.AlterField(
+ model_name='post',
+ name='content',
+ field=mdeditor.fields.MDTextField(),
+ ),
+ ]
diff --git a/myblog/blog/migrations/0005_remove_post_image.py b/myblog/blog/migrations/0005_remove_post_image.py
new file mode 100644
index 0000000..cab6f65
--- /dev/null
+++ b/myblog/blog/migrations/0005_remove_post_image.py
@@ -0,0 +1,17 @@
+# Generated by Django 5.2.4 on 2025-07-26 15:37
+
+from django.db import migrations
+
+
+class Migration(migrations.Migration):
+
+ dependencies = [
+ ('blog', '0004_alter_post_content'),
+ ]
+
+ operations = [
+ migrations.RemoveField(
+ model_name='post',
+ name='image',
+ ),
+ ]
diff --git a/myblog/blog/models.py b/myblog/blog/models.py
index e314f83..1bcf68a 100644
--- a/myblog/blog/models.py
+++ b/myblog/blog/models.py
@@ -1,20 +1,63 @@
+from django.conf import settings
from django.db import models
from django.utils import timezone
import markdown
from django.utils.safestring import mark_safe
+from mdeditor.fields import MDTextField
-# Create your models here.
class Post(models.Model):
title = models.CharField(max_length=100)
- content = models.TextField()
+ content = MDTextField() # ✅ 改成这里
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)
- # 修改publish_date字段,使其在后台可编辑
publish_date = models.DateTimeField(default=timezone.now)
def __str__(self):
return f"{self.title} ({self.publish_date.strftime('%Y-%m-%d')})"
def get_markdown_content(self):
- return mark_safe(markdown.markdown(self.content))
+ import re
+ content = self.content
+ media_url = settings.MEDIA_URL.rstrip('/')
+
+ # 统一处理所有可能的图片路径格式
+ # 处理Markdown格式的图片 
+ def replace_md_image(match):
+ alt_text = match.group(1)
+ img_path = match.group(2)
+ # 如果路径已经包含媒体URL则不处理
+ if img_path.startswith(media_url):
+ return match.group(0)
+ # 如果是相对路径,则添加媒体URL前缀
+ if not img_path.startswith(('http://', 'https://', '/')):
+ return f''
+ return match.group(0)
+
+ # 处理HTML格式的图片
+ def replace_html_image(match):
+ quote = match.group(1) or ''
+ img_path = match.group(2)
+ # 如果路径已经包含媒体URL则不处理
+ if img_path.startswith(media_url):
+ return match.group(0)
+ # 如果是相对路径,则添加媒体URL前缀
+ if not img_path.startswith(('http://', 'https://', '/')):
+ return f'src="{media_url}/{img_path}"'
+ return match.group(0)
+
+ # 使用函数替换处理所有Markdown图片
+ content = re.sub(
+ r'!\[([^\]]*)\]\(([^)]+)\)',
+ replace_md_image,
+ content
+ )
+
+ # 使用函数替换处理所有HTML图片
+ content = re.sub(
+ r'src=(["\']?)([^"\'>\s]+)\1',
+ replace_html_image,
+ content
+ )
+
+ return mark_safe(markdown.markdown(content))
diff --git a/myblog/blog/templates/blog/detail.html b/myblog/blog/templates/blog/detail.html
index 2ba4265..9158afa 100644
--- a/myblog/blog/templates/blog/detail.html
+++ b/myblog/blog/templates/blog/detail.html
@@ -7,15 +7,31 @@
+
+
六桂流芳的com
{{ post.title }}
-
发布时间:{{ post.publish_date|date:"Y年n月j日 H:i" }}
-
-
{{ post.get_markdown_content|safe }}
+
+
+
发布时间:{{ post.publish_date|date:"Y年n月j日 H:i" }}
+
返回首页
+
+
+
+
+
{{ post.get_markdown_content }}
+
-
返回首页
-