Compare commits
20 Commits
320cdc974e
...
ee406be52b
| Author | SHA256 | Date | |
|---|---|---|---|
| ee406be52b | |||
| 08ddfd6391 | |||
| 60af0f4ae3 | |||
| 479f59f0f3 | |||
| bbc3fcef08 | |||
| a32cd6aadd | |||
| 8401704aa6 | |||
| 5c1f5f81d3 | |||
| 010b45c328 | |||
| 3f9847d6fb | |||
| a8acfaf5b5 | |||
| 7bcc6460c4 | |||
| 67bc42e862 | |||
| cb64aa0911 | |||
| dbc3dac253 | |||
| 5a585b4fc9 | |||
| 84e649081a | |||
| 99cb1d7170 | |||
| 0cb2b6e621 | |||
| 1c7d752a57 |
3
.gitignore
vendored
3
.gitignore
vendored
@@ -174,3 +174,6 @@ cython_debug/
|
||||
# PyPI configuration file
|
||||
.pypirc
|
||||
|
||||
# 忽略媒体上传目录
|
||||
*/media/*
|
||||
|
||||
|
||||
8
.idea/.gitignore
generated
vendored
Normal file
8
.idea/.gitignore
generated
vendored
Normal file
@@ -0,0 +1,8 @@
|
||||
# 默认忽略的文件
|
||||
/shelf/
|
||||
/workspace.xml
|
||||
# 基于编辑器的 HTTP 客户端请求
|
||||
/httpRequests/
|
||||
# Datasource local storage ignored files
|
||||
/dataSources/
|
||||
/dataSources.local.xml
|
||||
30
.idea/blog.iml
generated
Normal file
30
.idea/blog.iml
generated
Normal file
@@ -0,0 +1,30 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<module type="PYTHON_MODULE" version="4">
|
||||
<component name="FacetManager">
|
||||
<facet type="django" name="Django">
|
||||
<configuration>
|
||||
<option name="rootFolder" value="$MODULE_DIR$/myblog" />
|
||||
<option name="settingsModule" value="myblog/settings.py" />
|
||||
<option name="manageScript" value="$MODULE_DIR$/myblog/manage.py" />
|
||||
<option name="environment" value="<map/>" />
|
||||
<option name="doNotUseTestRunner" value="false" />
|
||||
<option name="trackFilePattern" value="migrations" />
|
||||
</configuration>
|
||||
</facet>
|
||||
</component>
|
||||
<component name="NewModuleRootManager">
|
||||
<content url="file://$MODULE_DIR$">
|
||||
<sourceFolder url="file://$MODULE_DIR$/myblog" isTestSource="false" />
|
||||
<excludeFolder url="file://$MODULE_DIR$/venv" />
|
||||
</content>
|
||||
<orderEntry type="jdk" jdkName="Python 3.12 (blog) (2)" jdkType="Python SDK" />
|
||||
<orderEntry type="sourceFolder" forTests="false" />
|
||||
</component>
|
||||
<component name="PyDocumentationSettings">
|
||||
<option name="format" value="PLAIN" />
|
||||
<option name="myDocStringFormat" value="Plain" />
|
||||
</component>
|
||||
<component name="TemplatesService">
|
||||
<option name="TEMPLATE_CONFIGURATION" value="Django" />
|
||||
</component>
|
||||
</module>
|
||||
6
.idea/inspectionProfiles/profiles_settings.xml
generated
Normal file
6
.idea/inspectionProfiles/profiles_settings.xml
generated
Normal file
@@ -0,0 +1,6 @@
|
||||
<component name="InspectionProjectProfileManager">
|
||||
<settings>
|
||||
<option name="USE_PROJECT_PROFILE" value="false" />
|
||||
<version value="1.0" />
|
||||
</settings>
|
||||
</component>
|
||||
7
.idea/misc.xml
generated
Normal file
7
.idea/misc.xml
generated
Normal file
@@ -0,0 +1,7 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="Black">
|
||||
<option name="sdkName" value="Python 3.12 (blog) (2)" />
|
||||
</component>
|
||||
<component name="ProjectRootManager" version="2" project-jdk-name="Python 3.12 (blog) (2)" project-jdk-type="Python SDK" />
|
||||
</project>
|
||||
8
.idea/modules.xml
generated
Normal file
8
.idea/modules.xml
generated
Normal file
@@ -0,0 +1,8 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="ProjectModuleManager">
|
||||
<modules>
|
||||
<module fileurl="file://$PROJECT_DIR$/.idea/blog.iml" filepath="$PROJECT_DIR$/.idea/blog.iml" />
|
||||
</modules>
|
||||
</component>
|
||||
</project>
|
||||
6
.idea/vcs.xml
generated
Normal file
6
.idea/vcs.xml
generated
Normal file
@@ -0,0 +1,6 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="VcsDirectoryMappings">
|
||||
<mapping directory="" vcs="Git" />
|
||||
</component>
|
||||
</project>
|
||||
@@ -1,3 +1,20 @@
|
||||
from django.contrib import admin
|
||||
from .models import Post
|
||||
from django.db import models
|
||||
from mdeditor.widgets import MDEditorWidget
|
||||
|
||||
# Register your models here.
|
||||
|
||||
class PostAdmin(admin.ModelAdmin):
|
||||
# 使用MDEditor Markdown编辑器替换默认的Textarea
|
||||
formfield_overrides = {
|
||||
models.TextField: {'widget': MDEditorWidget},
|
||||
}
|
||||
|
||||
# 设置列表显示字段
|
||||
list_display = ('title', 'publish_date', 'created_at', 'updated_at')
|
||||
# 设置搜索字段
|
||||
search_fields = ('title', 'content')
|
||||
|
||||
|
||||
# 注册自定义的PostAdmin
|
||||
admin.site.register(Post, PostAdmin)
|
||||
|
||||
24
myblog/blog/migrations/0001_initial.py
Normal file
24
myblog/blog/migrations/0001_initial.py
Normal file
@@ -0,0 +1,24 @@
|
||||
# Generated by Django 5.2.4 on 2025-07-26 10:29
|
||||
|
||||
from django.db import migrations, models
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
initial = True
|
||||
|
||||
dependencies = [
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.CreateModel(
|
||||
name='Post',
|
||||
fields=[
|
||||
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
|
||||
('title', models.CharField(max_length=100)),
|
||||
('content', models.TextField()),
|
||||
('created_at', models.DateTimeField(auto_now_add=True)),
|
||||
('updated_at', models.DateTimeField(auto_now=True)),
|
||||
],
|
||||
),
|
||||
]
|
||||
19
myblog/blog/migrations/0002_post_publish_date.py
Normal file
19
myblog/blog/migrations/0002_post_publish_date.py
Normal file
@@ -0,0 +1,19 @@
|
||||
# Generated by Django 5.2.4 on 2025-07-26 12:10
|
||||
|
||||
import django.utils.timezone
|
||||
from django.db import migrations, models
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('blog', '0001_initial'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.AddField(
|
||||
model_name='post',
|
||||
name='publish_date',
|
||||
field=models.DateTimeField(default=django.utils.timezone.now),
|
||||
),
|
||||
]
|
||||
18
myblog/blog/migrations/0003_post_image.py
Normal file
18
myblog/blog/migrations/0003_post_image.py
Normal file
@@ -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/'),
|
||||
),
|
||||
]
|
||||
19
myblog/blog/migrations/0004_alter_post_content.py
Normal file
19
myblog/blog/migrations/0004_alter_post_content.py
Normal file
@@ -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(),
|
||||
),
|
||||
]
|
||||
17
myblog/blog/migrations/0005_remove_post_image.py
Normal file
17
myblog/blog/migrations/0005_remove_post_image.py
Normal file
@@ -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',
|
||||
),
|
||||
]
|
||||
@@ -1,3 +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 = MDTextField() # ✅ 改成这里
|
||||
created_at = models.DateTimeField(auto_now_add=True)
|
||||
updated_at = models.DateTimeField(auto_now=True)
|
||||
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):
|
||||
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格式的图片 <img src="path">
|
||||
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))
|
||||
|
||||
37
myblog/blog/templates/blog/detail.html
Normal file
37
myblog/blog/templates/blog/detail.html
Normal file
@@ -0,0 +1,37 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="zh">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<title>{{ post.title }}</title>
|
||||
</head>
|
||||
<body>
|
||||
<!-- 添加样式使整个页面内容居中显示 -->
|
||||
<div style="max-width: 800px; margin: 0 auto; padding: 0 20px;">
|
||||
<!-- 添加网站标题 -->
|
||||
<h1 style="text-align: left;">六桂流芳的com</h1>
|
||||
<h1 style="text-align: center;">{{ post.title }}</h1>
|
||||
<!-- 将返回首页链接放在发布时间的右侧 -->
|
||||
<div style="display: flex; align-items: center; justify-content: space-between;">
|
||||
<p>发布时间:{{ post.publish_date|date:"Y年n月j日 H:i" }}</p>
|
||||
<a href="{% url 'index' %}" style="margin-left: 20px; white-space: nowrap;">返回首页</a>
|
||||
</div>
|
||||
<!-- 添加CSS样式限制图片大小 -->
|
||||
<div style="max-width: 100%;">
|
||||
<style>
|
||||
.post-content img {
|
||||
max-width: 100%;
|
||||
height: auto;
|
||||
display: block;
|
||||
margin: 10px 0;
|
||||
}
|
||||
</style>
|
||||
<div class="post-content">{{ post.get_markdown_content }}</div>
|
||||
</div>
|
||||
<br>
|
||||
</div>
|
||||
<footer style="position: fixed; bottom: 0; width: 100%; text-align: center; font-size: 12px; color: #999; background-color: white; padding: 5px 0;">
|
||||
<a href="https://beian.miit.gov.cn/" target="_blank">闽ICP备2023010767号-2</a>
|
||||
</footer>
|
||||
</body>
|
||||
</html>
|
||||
|
||||
26
myblog/blog/templates/blog/index.html
Normal file
26
myblog/blog/templates/blog/index.html
Normal file
@@ -0,0 +1,26 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="zh">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<title>六桂流芳的com</title>
|
||||
</head>
|
||||
<body>
|
||||
<!-- 修改样式使页面内容靠左对齐,标题显示在左上角 -->
|
||||
<div style="max-width: 800px; margin: 0 auto; padding: 0 20px; text-align: left;">
|
||||
<h1 style="text-align: left;">六桂流芳的com</h1>
|
||||
<ul style="list-style: none; padding: 0;">
|
||||
{% for post in posts %}
|
||||
<li style="margin: 10px 0; text-align: left;">
|
||||
<div style="display: flex; justify-content: space-between; align-items: center;">
|
||||
<a href="{% url 'detail' post.id %}">{{ post.title }}</a>
|
||||
<span style="margin-left: 10px; white-space: nowrap;">发布时间:{{ post.publish_date|date:"Y年n月j日 H:i" }}</span>
|
||||
</div>
|
||||
</li>
|
||||
{% endfor %}
|
||||
</ul>
|
||||
</div>
|
||||
<footer style="position: fixed; bottom: 0; width: 100%; text-align: center; font-size: 12px; color: #999; background-color: white; padding: 5px 0;">
|
||||
<a href="https://beian.miit.gov.cn/" target="_blank">闽ICP备2023010767号-2</a>
|
||||
</footer>
|
||||
</body>
|
||||
</html>
|
||||
8
myblog/blog/urls.py
Normal file
8
myblog/blog/urls.py
Normal file
@@ -0,0 +1,8 @@
|
||||
from django.urls import path
|
||||
|
||||
from . import views
|
||||
|
||||
urlpatterns = [
|
||||
path('', views.index, name='index'),
|
||||
path('post/<int:post_id>/', views.detail, name='detail'),
|
||||
]
|
||||
@@ -1,3 +1,14 @@
|
||||
from django.shortcuts import render
|
||||
from django.shortcuts import render, get_object_or_404
|
||||
from .models import Post
|
||||
|
||||
|
||||
# Create your views here.
|
||||
|
||||
def index(request):
|
||||
posts = Post.objects.order_by('created_at')
|
||||
return render(request, 'blog/index.html', {'posts': posts})
|
||||
|
||||
|
||||
def detail(request, post_id):
|
||||
post = get_object_or_404(Post, pk=post_id)
|
||||
return render(request, 'blog/detail.html', {'post': post})
|
||||
|
||||
@@ -15,7 +15,6 @@ from pathlib import Path
|
||||
# Build paths inside the project like this: BASE_DIR / 'subdir'.
|
||||
BASE_DIR = Path(__file__).resolve().parent.parent
|
||||
|
||||
|
||||
# Quick-start development settings - unsuitable for production
|
||||
# See https://docs.djangoproject.com/en/5.2/howto/deployment/checklist/
|
||||
|
||||
@@ -27,7 +26,6 @@ DEBUG = True
|
||||
|
||||
ALLOWED_HOSTS = []
|
||||
|
||||
|
||||
# Application definition
|
||||
|
||||
INSTALLED_APPS = [
|
||||
@@ -37,6 +35,8 @@ INSTALLED_APPS = [
|
||||
'django.contrib.sessions',
|
||||
'django.contrib.messages',
|
||||
'django.contrib.staticfiles',
|
||||
'blog',
|
||||
'mdeditor',
|
||||
]
|
||||
|
||||
MIDDLEWARE = [
|
||||
@@ -68,7 +68,6 @@ TEMPLATES = [
|
||||
|
||||
WSGI_APPLICATION = 'myblog.wsgi.application'
|
||||
|
||||
|
||||
# Database
|
||||
# https://docs.djangoproject.com/en/5.2/ref/settings/#databases
|
||||
|
||||
@@ -79,7 +78,6 @@ DATABASES = {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
# Password validation
|
||||
# https://docs.djangoproject.com/en/5.2/ref/settings/#auth-password-validators
|
||||
|
||||
@@ -98,25 +96,64 @@ AUTH_PASSWORD_VALIDATORS = [
|
||||
},
|
||||
]
|
||||
|
||||
|
||||
# Internationalization
|
||||
# https://docs.djangoproject.com/en/5.2/topics/i18n/
|
||||
|
||||
LANGUAGE_CODE = 'en-us'
|
||||
|
||||
TIME_ZONE = 'UTC'
|
||||
LANGUAGE_CODE = 'zh-Hans'
|
||||
TIME_ZONE = 'Asia/Shanghai'
|
||||
|
||||
USE_I18N = True
|
||||
|
||||
USE_TZ = True
|
||||
|
||||
|
||||
# Static files (CSS, JavaScript, Images)
|
||||
# https://docs.djangoproject.com/en/5.2/howto/static-files/
|
||||
|
||||
STATIC_URL = 'static/'
|
||||
|
||||
# 添加媒体文件配置
|
||||
import os
|
||||
|
||||
MEDIA_URL = '/media/'
|
||||
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
|
||||
|
||||
# Default primary key field type
|
||||
# https://docs.djangoproject.com/en/5.2/ref/settings/#default-auto-field
|
||||
|
||||
DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'
|
||||
|
||||
CSRF_TRUSTED_ORIGINS = [
|
||||
"https://www.yuangyaa.com",
|
||||
"http://www.yuangyaa.com",
|
||||
"http://yuangyaa.com",
|
||||
"https://yuangyaa.com",
|
||||
]
|
||||
|
||||
# 添加 MDEditor 配置
|
||||
MDEDITOR_CONFIGS = {
|
||||
'default': {
|
||||
'width': '100%',
|
||||
'height': 700,
|
||||
'toolbar': ["undo", "redo", "|",
|
||||
"bold", "del", "italic", "quote", "ucwords", "uppercase", "lowercase", "|",
|
||||
"h1", "h2", "h3", "h5", "h6", "|",
|
||||
"list-ul", "list-ol", "hr", "|",
|
||||
"link", "reference-link", "image", "code", "preformatted-text", "code-block", "table", "datetime",
|
||||
"emoji", "html-entities", "pagebreak", "goto-line", "|", "help", "info",
|
||||
"||", "preview", "watch", "fullscreen"],
|
||||
'upload_image_formats': ["jpg", "jpeg", "gif", "png", "bmp", "webp"],
|
||||
'image_folder': 'editor',
|
||||
'theme': 'default',
|
||||
'preview_theme': 'default',
|
||||
'editor_theme': 'default',
|
||||
'toolbar_autofixed': True,
|
||||
'search_replace': True,
|
||||
'emoji': True,
|
||||
'tex': False,
|
||||
'language': 'zh',
|
||||
'focus': False,
|
||||
'auto_height': False,
|
||||
}
|
||||
}
|
||||
|
||||
X_FRAME_OPTIONS = 'SAMEORIGIN'
|
||||
|
||||
@@ -15,8 +15,17 @@ Including another URLconf
|
||||
2. Add a URL to urlpatterns: path('blog/', include('blog.urls'))
|
||||
"""
|
||||
from django.contrib import admin
|
||||
from django.urls import path
|
||||
from django.urls import path, include
|
||||
from django.conf import settings
|
||||
from django.conf.urls.static import static
|
||||
|
||||
urlpatterns = [
|
||||
path('admin/', admin.site.urls),
|
||||
path('mdeditor/', include('mdeditor.urls')),
|
||||
# 包含blog应用的URL
|
||||
path('', include('blog.urls')),
|
||||
]
|
||||
|
||||
# 添加媒体文件URL配置 - 确保在DEBUG和生产环境都能正确处理媒体文件
|
||||
if settings.DEBUG:
|
||||
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
|
||||
|
||||
41
myblog/requirements.txt
Normal file
41
myblog/requirements.txt
Normal file
@@ -0,0 +1,41 @@
|
||||
asgiref==3.9.1
|
||||
asttokens==3.0.0
|
||||
bleach==6.2.0
|
||||
certifi==2025.7.14
|
||||
charset-normalizer==3.4.2
|
||||
click==8.2.1
|
||||
decorator==5.2.1
|
||||
Django==5.2.4
|
||||
executing==2.2.0
|
||||
gunicorn==23.0.0
|
||||
h11==0.16.0
|
||||
idna==3.10
|
||||
ipython==9.4.0
|
||||
ipython_pygments_lexers==1.1.1
|
||||
jedi==0.19.2
|
||||
Markdown==3.5.2
|
||||
martor==1.6.45
|
||||
matplotlib-inline==0.1.7
|
||||
numpy==2.3.2
|
||||
packaging==25.0
|
||||
pandas==2.3.1
|
||||
parso==0.8.4
|
||||
pexpect==4.9.0
|
||||
prompt_toolkit==3.0.51
|
||||
psycopg2-binary==2.9.10
|
||||
ptyprocess==0.7.0
|
||||
pure_eval==0.2.3
|
||||
Pygments==2.19.2
|
||||
python-dateutil==2.9.0.post0
|
||||
pytz==2025.2
|
||||
requests==2.32.4
|
||||
six==1.17.0
|
||||
sqlparse==0.5.3
|
||||
stack-data==0.6.3
|
||||
traitlets==5.14.3
|
||||
tzdata==2025.2
|
||||
urllib3==2.5.0
|
||||
uv==0.8.3
|
||||
uvicorn==0.35.0
|
||||
wcwidth==0.2.13
|
||||
webencodings==0.5.1
|
||||
Reference in New Issue
Block a user