diff --git a/myblog/blog/admin.py b/myblog/blog/admin.py
index 97e651b..b0ca294 100644
--- a/myblog/blog/admin.py
+++ b/myblog/blog/admin.py
@@ -1,3 +1,20 @@
from django.contrib import admin
+from .models import Post
+from django.db import models
+from martor.widgets import AdminMartorWidget
-# Register your models here.
+
+class PostAdmin(admin.ModelAdmin):
+ # 使用Martor Markdown编辑器替换默认的Textarea
+ formfield_overrides = {
+ models.TextField: {'widget': AdminMartorWidget},
+ }
+
+ # 设置列表显示字段
+ list_display = ('title', 'publish_date', 'created_at')
+ # 设置搜索字段
+ search_fields = ('title', 'content')
+
+
+# 注册自定义的PostAdmin
+admin.site.register(Post, PostAdmin)
diff --git a/myblog/blog/migrations/0001_initial.py b/myblog/blog/migrations/0001_initial.py
new file mode 100644
index 0000000..89e7300
--- /dev/null
+++ b/myblog/blog/migrations/0001_initial.py
@@ -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)),
+ ],
+ ),
+ ]
diff --git a/myblog/blog/migrations/0002_post_publish_date.py b/myblog/blog/migrations/0002_post_publish_date.py
new file mode 100644
index 0000000..286041d
--- /dev/null
+++ b/myblog/blog/migrations/0002_post_publish_date.py
@@ -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),
+ ),
+ ]
diff --git a/myblog/blog/models.py b/myblog/blog/models.py
index 5d0d3b0..e314f83 100644
--- a/myblog/blog/models.py
+++ b/myblog/blog/models.py
@@ -1,3 +1,20 @@
from django.db import models
+from django.utils import timezone
+import markdown
+from django.utils.safestring import mark_safe
+
# Create your models here.
+class Post(models.Model):
+ title = models.CharField(max_length=100)
+ content = models.TextField()
+ 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))
diff --git a/myblog/blog/templates/blog/detail.html b/myblog/blog/templates/blog/detail.html
new file mode 100644
index 0000000..2ba4265
--- /dev/null
+++ b/myblog/blog/templates/blog/detail.html
@@ -0,0 +1,21 @@
+
+
+
+
+ {{ post.title }}
+
+
+
+
+
{{ post.title }}
+
发布时间:{{ post.publish_date|date:"Y年n月j日 H:i" }}
+
+
{{ post.get_markdown_content|safe }}
+
+
返回首页
+
+
+
+
\ No newline at end of file
diff --git a/myblog/blog/templates/blog/index.html b/myblog/blog/templates/blog/index.html
new file mode 100644
index 0000000..efb794d
--- /dev/null
+++ b/myblog/blog/templates/blog/index.html
@@ -0,0 +1,27 @@
+
+
+
+
+ 六桂流芳的com
+
+
+
+
+
六桂流芳的com
+
+ {% for post in posts %}
+ -
+ {{ post.title }} -
+ 发布时间:{{ post.publish_date|date:"Y年n月j日 H:i" }}
+
+ {% endfor %}
+
+
+
+
+
+
+
+
diff --git a/myblog/blog/urls.py b/myblog/blog/urls.py
new file mode 100644
index 0000000..1c41b7d
--- /dev/null
+++ b/myblog/blog/urls.py
@@ -0,0 +1,8 @@
+from django.urls import path
+
+from . import views
+
+urlpatterns = [
+ path('', views.index, name='index'),
+ path('post//', views.detail, name='detail'),
+]
diff --git a/myblog/blog/views.py b/myblog/blog/views.py
index 244b489..38b5198 100644
--- a/myblog/blog/views.py
+++ b/myblog/blog/views.py
@@ -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})
diff --git a/myblog/myblog/settings.py b/myblog/myblog/settings.py
index 4fd067f..69a5669 100644
--- a/myblog/myblog/settings.py
+++ b/myblog/myblog/settings.py
@@ -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',
+ 'martor',
]
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,19 +96,16 @@ 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/
@@ -120,3 +115,10 @@ STATIC_URL = 'static/'
# 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",
+]
diff --git a/myblog/myblog/urls.py b/myblog/myblog/urls.py
index 624673c..160654a 100644
--- a/myblog/myblog/urls.py
+++ b/myblog/myblog/urls.py
@@ -15,8 +15,13 @@ 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
+
urlpatterns = [
path('admin/', admin.site.urls),
+ # 添加martor的URL配置以支持Markdown编辑器
+ path('martor/', include('martor.urls')),
+ # 包含blog应用的URL
+ path('', include('blog.urls')),
]
diff --git a/myblog/requirements.txt b/myblog/requirements.txt
new file mode 100644
index 0000000..38d3df2
--- /dev/null
+++ b/myblog/requirements.txt
@@ -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