The blog base view

This commit is contained in:
2025-07-26 18:36:16 +08:00
parent 0cb2b6e621
commit 5a585b4fc9
9 changed files with 121 additions and 11 deletions

View File

@@ -1,3 +1,6 @@
from django.contrib import admin from django.contrib import admin
from .models import Post
# Register your models here. # Register your models here.
admin.site.register(Post)

View 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)),
],
),
]

View File

@@ -1,3 +1,12 @@
from django.db import models from django.db import models
# Create your models here. # 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)
def __str__(self):
return self.title

View File

@@ -0,0 +1,25 @@
<!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; text-align: center;">
<!-- 添加网站标题 -->
<h1>{{ post.title }}</h1>
<p>{{ post.created_at }}</p>
<div>{{ post.content|linebreaks }}</div>
<p><a href="/">← 返回首页</a></p>
<footer style="margin-top: 40px; font-size: 12px; color: #999; text-align: center;">
<a href="https://beian.miit.gov.cn/" target="_blank">闽ICP备2023010767号-2</a>
</footer>
<!-- 删除:<p><a href="/">← 返回首页</a></p> -->
</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>

View File

@@ -0,0 +1,27 @@
<!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: center;">
<h1>六桂流芳的com</h1>
<ul style="list-style: none; padding: 0;">
{% for post in posts %}
<li style="margin: 10px 0; text-align: left;">
<a href="{% url 'detail' post.id %}">{{ post.title }}</a> -
发布时间:{{ post.published_at|date:"Y年n月j日 H:i" }}
</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
View 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'),
]

View File

@@ -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. # 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})

View File

@@ -15,7 +15,6 @@ from pathlib import Path
# Build paths inside the project like this: BASE_DIR / 'subdir'. # Build paths inside the project like this: BASE_DIR / 'subdir'.
BASE_DIR = Path(__file__).resolve().parent.parent BASE_DIR = Path(__file__).resolve().parent.parent
# Quick-start development settings - unsuitable for production # Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/5.2/howto/deployment/checklist/ # See https://docs.djangoproject.com/en/5.2/howto/deployment/checklist/
@@ -27,7 +26,6 @@ DEBUG = True
ALLOWED_HOSTS = [] ALLOWED_HOSTS = []
# Application definition # Application definition
INSTALLED_APPS = [ INSTALLED_APPS = [
@@ -37,6 +35,8 @@ INSTALLED_APPS = [
'django.contrib.sessions', 'django.contrib.sessions',
'django.contrib.messages', 'django.contrib.messages',
'django.contrib.staticfiles', 'django.contrib.staticfiles',
'blog'
] ]
MIDDLEWARE = [ MIDDLEWARE = [
@@ -68,7 +68,6 @@ TEMPLATES = [
WSGI_APPLICATION = 'myblog.wsgi.application' WSGI_APPLICATION = 'myblog.wsgi.application'
# Database # Database
# https://docs.djangoproject.com/en/5.2/ref/settings/#databases # https://docs.djangoproject.com/en/5.2/ref/settings/#databases
@@ -79,7 +78,6 @@ DATABASES = {
} }
} }
# Password validation # Password validation
# https://docs.djangoproject.com/en/5.2/ref/settings/#auth-password-validators # https://docs.djangoproject.com/en/5.2/ref/settings/#auth-password-validators
@@ -98,19 +96,16 @@ AUTH_PASSWORD_VALIDATORS = [
}, },
] ]
# Internationalization # Internationalization
# https://docs.djangoproject.com/en/5.2/topics/i18n/ # https://docs.djangoproject.com/en/5.2/topics/i18n/
LANGUAGE_CODE = 'en-us' LANGUAGE_CODE = 'zh-Hans'
TIME_ZONE = 'Asia/Shanghai'
TIME_ZONE = 'UTC'
USE_I18N = True USE_I18N = True
USE_TZ = True USE_TZ = True
# Static files (CSS, JavaScript, Images) # Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/5.2/howto/static-files/ # 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 # https://docs.djangoproject.com/en/5.2/ref/settings/#default-auto-field
DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField' DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'
CSRF_TRUSTED_ORIGINS = [
"https://www.yuangyaa.com",
"http://www.yuangyaa.com",
"http://yuangyaa.com",
"https://yuangyaa.com",
]

View File

@@ -15,8 +15,9 @@ Including another URLconf
2. Add a URL to urlpatterns: path('blog/', include('blog.urls')) 2. Add a URL to urlpatterns: path('blog/', include('blog.urls'))
""" """
from django.contrib import admin from django.contrib import admin
from django.urls import path from django.urls import path, include
urlpatterns = [ urlpatterns = [
path('admin/', admin.site.urls), path('admin/', admin.site.urls),
path('', include('blog.urls')),
] ]