初始创建博客站点的迁移骨架

This commit is contained in:
2026-10-20 00:09:46 +08:00
commit 9592e6a150
18 changed files with 315 additions and 0 deletions
View File
+3
View File
@@ -0,0 +1,3 @@
from django.contrib import admin
# Register your models here.
+6
View File
@@ -0,0 +1,6 @@
from django.apps import AppConfig
class BlogConfig(AppConfig):
default_auto_field = "django.db.models.BigAutoField"
name = "blog"
View File
+84
View File
@@ -0,0 +1,84 @@
"""
博客文章数据模型
这个文件定义了数据库表结构
Django ORM 会根据这里的类自动建表
"""
from django.db import models
from django.urls import reverse
class Post(models.Model):
"""
博客文章模型
对应数据库里的 blog_post 表
字段设计参考 WordPress 的 wp_posts 表结构
"""
# 文章标题,CharField 对应 VARCHAR
# max_length=200 是数据库字段长度限制
# verbose_name 是后台显示的名称
title = models.CharField(max_length=200, verbose_name='标题')
# 别名(slug),用于 URL
# 比如文章标题是 "Hello World",slug 就是 "hello-world"
# URL 变成 /hello-world/ 而不是 /123/
slug = models.SlugField(
max_length=200,
unique=True, # 不能重复
verbose_name='URL别名'
)
# 文章内容,TextField 对应 TEXT 类型,无长度限制
# WordPress 里存的是 HTML
content = models.TextField(verbose_name='内容')
# 摘要,允许为空
excerpt = models.TextField(
blank=True, # 表单中允许为空
default='', # 默认值
verbose_name='摘要'
)
# 发布时间,用 DateTimeField
# 对应 WordPress 的 post_date
published_at = models.DateTimeField(verbose_name='发布时间')
# 创建时间和更新时间(自动维护)
created_at = models.DateTimeField(auto_now_add=True) # 首次创建时自动填入
updated_at = models.DateTimeField(auto_now=True) # 每次保存时自动更新
# 文章状态
STATUS_CHOICES = [
('draft', '草稿'), # 还没写完
('published', '已发布'), # 公开可见
]
status = models.CharField(
max_length=20,
choices=STATUS_CHOICES, # 只能从列表里选
default='published',
verbose_name='状态'
)
class Meta:
"""
Meta 类用来配置模型级别的选项
"""
ordering = ['-published_at'] # 默认按发布时间倒序
verbose_name = '文章' # 后台单数名称
verbose_name_plural = '文章' # 后台复数名称
db_table = 'blog_post' # 指定表名(默认是 app名_类名)
def __str__(self):
"""
返回对象的字符串表示
在后台列表、shell 里显示时用
"""
return self.title
def get_absolute_url(self):
"""
返回这篇文章的详情页 URL
reverse 会根据 urls.py 里的 name 反查出路径
"""
return reverse('blog:post_detail', kwargs={'slug': self.slug})
+3
View File
@@ -0,0 +1,3 @@
from django.test import TestCase
# Create your tests here.
+3
View File
@@ -0,0 +1,3 @@
from django.shortcuts import render
# Create your views here.