commit 9592e6a150b2edae6701a141ebd980f10833b7aa Author: xiaoan <419177984@qq.com> Date: Tue Oct 20 00:09:46 2026 +0800 初始创建博客站点的迁移骨架 diff --git a/blog/__init__.py b/blog/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/blog/admin.py b/blog/admin.py new file mode 100644 index 0000000..8c38f3f --- /dev/null +++ b/blog/admin.py @@ -0,0 +1,3 @@ +from django.contrib import admin + +# Register your models here. diff --git a/blog/apps.py b/blog/apps.py new file mode 100644 index 0000000..6be26c7 --- /dev/null +++ b/blog/apps.py @@ -0,0 +1,6 @@ +from django.apps import AppConfig + + +class BlogConfig(AppConfig): + default_auto_field = "django.db.models.BigAutoField" + name = "blog" diff --git a/blog/migrations/__init__.py b/blog/migrations/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/blog/models.py b/blog/models.py new file mode 100644 index 0000000..7ba07ce --- /dev/null +++ b/blog/models.py @@ -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}) \ No newline at end of file diff --git a/blog/tests.py b/blog/tests.py new file mode 100644 index 0000000..7ce503c --- /dev/null +++ b/blog/tests.py @@ -0,0 +1,3 @@ +from django.test import TestCase + +# Create your tests here. diff --git a/blog/views.py b/blog/views.py new file mode 100644 index 0000000..91ea44a --- /dev/null +++ b/blog/views.py @@ -0,0 +1,3 @@ +from django.shortcuts import render + +# Create your views here. diff --git a/blogsite/__init__.py b/blogsite/__init__.py new file mode 100644 index 0000000..ce41b34 --- /dev/null +++ b/blogsite/__init__.py @@ -0,0 +1,9 @@ +""" +这个文件是项目的包初始化文件 +在这里做数据库驱动的猴子补丁(monkey patch) +让 Django 以为 pymysql 就是 MySQLdb +""" +import pymysql +# 把 pymysql 伪装成 MySQLdb +# Django 内部 import MySQLdb,实际用的是 pymysql +pymysql.install_as_MySQLdb() \ No newline at end of file diff --git a/blogsite/__pycache__/__init__.cpython-310.pyc b/blogsite/__pycache__/__init__.cpython-310.pyc new file mode 100644 index 0000000..25397d0 Binary files /dev/null and b/blogsite/__pycache__/__init__.cpython-310.pyc differ diff --git a/blogsite/__pycache__/settings.cpython-310.pyc b/blogsite/__pycache__/settings.cpython-310.pyc new file mode 100644 index 0000000..77b4693 Binary files /dev/null and b/blogsite/__pycache__/settings.cpython-310.pyc differ diff --git a/blogsite/__pycache__/urls.cpython-310.pyc b/blogsite/__pycache__/urls.cpython-310.pyc new file mode 100644 index 0000000..8dcad15 Binary files /dev/null and b/blogsite/__pycache__/urls.cpython-310.pyc differ diff --git a/blogsite/__pycache__/wsgi.cpython-310.pyc b/blogsite/__pycache__/wsgi.cpython-310.pyc new file mode 100644 index 0000000..8311558 Binary files /dev/null and b/blogsite/__pycache__/wsgi.cpython-310.pyc differ diff --git a/blogsite/asgi.py b/blogsite/asgi.py new file mode 100644 index 0000000..0f0b66b --- /dev/null +++ b/blogsite/asgi.py @@ -0,0 +1,16 @@ +""" +ASGI config for blogsite project. + +It exposes the ASGI callable as a module-level variable named ``application``. + +For more information on this file, see +https://docs.djangoproject.com/en/5.2/howto/deployment/asgi/ +""" + +import os + +from django.core.asgi import get_asgi_application + +os.environ.setdefault("DJANGO_SETTINGS_MODULE", "blogsite.settings") + +application = get_asgi_application() diff --git a/blogsite/settings.py b/blogsite/settings.py new file mode 100644 index 0000000..a8d7d19 --- /dev/null +++ b/blogsite/settings.py @@ -0,0 +1,130 @@ +""" +Django settings for blogsite project. + +Generated by 'django-admin startproject' using Django 5.2.17. + +For more information on this file, see +https://docs.djangoproject.com/en/5.2/topics/settings/ + +For the full list of settings and their values, see +https://docs.djangoproject.com/en/5.2/ref/settings/ +""" + +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/ + +# SECURITY WARNING: keep the secret key used in production secret! +SECRET_KEY = "django-insecure-v$4g@p+20)sq48$*7+@0gzv^=56vrfw+@3$_ge!d3yd2bu5zgc" + +# SECURITY WARNING: don't run with debug turned on in production! +DEBUG = True + +ALLOWED_HOSTS = ["*"] + + +# Application definition + +INSTALLED_APPS = [ + "django.contrib.admin", + "django.contrib.auth", + "django.contrib.contenttypes", + "django.contrib.sessions", + "django.contrib.messages", + "django.contrib.staticfiles", + "blog", +] + +MIDDLEWARE = [ + "django.middleware.security.SecurityMiddleware", + "django.contrib.sessions.middleware.SessionMiddleware", + "django.middleware.common.CommonMiddleware", + "django.middleware.csrf.CsrfViewMiddleware", + "django.contrib.auth.middleware.AuthenticationMiddleware", + "django.contrib.messages.middleware.MessageMiddleware", + "django.middleware.clickjacking.XFrameOptionsMiddleware", +] + +ROOT_URLCONF = "blogsite.urls" + +TEMPLATES = [ + { + "BACKEND": "django.template.backends.django.DjangoTemplates", + "DIRS": [], + "APP_DIRS": True, + "OPTIONS": { + "context_processors": [ + "django.template.context_processors.request", + "django.contrib.auth.context_processors.auth", + "django.contrib.messages.context_processors.messages", + ], + }, + }, +] + +WSGI_APPLICATION = "blogsite.wsgi.application" + + +# Database +# https://docs.djangoproject.com/en/5.2/ref/settings/#databases + +DATABASES = { + "default": { + "ENGINE": "django.db.backends.sqlite3", + "NAME": BASE_DIR / "db.sqlite3", + } +} + + +# Password validation +# https://docs.djangoproject.com/en/5.2/ref/settings/#auth-password-validators + +AUTH_PASSWORD_VALIDATORS = [ + { + "NAME": "django.contrib.auth.password_validation.UserAttributeSimilarityValidator", + }, + { + "NAME": "django.contrib.auth.password_validation.MinimumLengthValidator", + }, + { + "NAME": "django.contrib.auth.password_validation.CommonPasswordValidator", + }, + { + "NAME": "django.contrib.auth.password_validation.NumericPasswordValidator", + }, +] + + +# Internationalization +# https://docs.djangoproject.com/en/5.2/topics/i18n/ + +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/" + +# Default primary key field type +# https://docs.djangoproject.com/en/5.2/ref/settings/#default-auto-field + +DEFAULT_AUTO_FIELD = "django.db.models.BigAutoField" + +# 媒体文件配置(上传的图片等) +# MEDIA_URL:浏览器访问时的 URL 前缀 +MEDIA_URL = '/media/' +# MEDIA_ROOT:服务器上实际存储的路径(BASE_DIR 是项目根目录) +MEDIA_ROOT = BASE_DIR / 'media' diff --git a/blogsite/urls.py b/blogsite/urls.py new file mode 100644 index 0000000..37b7cab --- /dev/null +++ b/blogsite/urls.py @@ -0,0 +1,23 @@ +""" +URL configuration for blogsite project. + +The `urlpatterns` list routes URLs to views. For more information please see: + https://docs.djangoproject.com/en/5.2/topics/http/urls/ +Examples: +Function views + 1. Add an import: from my_app import views + 2. Add a URL to urlpatterns: path('', views.home, name='home') +Class-based views + 1. Add an import: from other_app.views import Home + 2. Add a URL to urlpatterns: path('', Home.as_view(), name='home') +Including another URLconf + 1. Import the include() function: from django.urls import include, path + 2. Add a URL to urlpatterns: path('blog/', include('blog.urls')) +""" + +from django.contrib import admin +from django.urls import path + +urlpatterns = [ + path("admin/", admin.site.urls), +] diff --git a/blogsite/wsgi.py b/blogsite/wsgi.py new file mode 100644 index 0000000..06f4c68 --- /dev/null +++ b/blogsite/wsgi.py @@ -0,0 +1,16 @@ +""" +WSGI config for blogsite project. + +It exposes the WSGI callable as a module-level variable named ``application``. + +For more information on this file, see +https://docs.djangoproject.com/en/5.2/howto/deployment/wsgi/ +""" + +import os + +from django.core.wsgi import get_wsgi_application + +os.environ.setdefault("DJANGO_SETTINGS_MODULE", "blogsite.settings") + +application = get_wsgi_application() diff --git a/db.sqlite3 b/db.sqlite3 new file mode 100644 index 0000000..e69de29 diff --git a/manage.py b/manage.py new file mode 100755 index 0000000..2f5f629 --- /dev/null +++ b/manage.py @@ -0,0 +1,22 @@ +#!/usr/bin/env python +"""Django's command-line utility for administrative tasks.""" +import os +import sys + + +def main(): + """Run administrative tasks.""" + os.environ.setdefault("DJANGO_SETTINGS_MODULE", "blogsite.settings") + try: + from django.core.management import execute_from_command_line + except ImportError as exc: + raise ImportError( + "Couldn't import Django. Are you sure it's installed and " + "available on your PYTHONPATH environment variable? Did you " + "forget to activate a virtual environment?" + ) from exc + execute_from_command_line(sys.argv) + + +if __name__ == "__main__": + main()