Merge feature/base-view into develop
This commit is contained in:
@@ -1,3 +1,6 @@
|
||||
from django.contrib import admin
|
||||
from .models import Post
|
||||
|
||||
# Register your models here.
|
||||
|
||||
admin.site.register(Post)
|
||||
|
||||
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)),
|
||||
],
|
||||
),
|
||||
]
|
||||
@@ -1,3 +1,12 @@
|
||||
from django.db import models
|
||||
|
||||
|
||||
# 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
|
||||
|
||||
25
myblog/blog/templates/blog/detail.html
Normal file
25
myblog/blog/templates/blog/detail.html
Normal 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>
|
||||
|
||||
27
myblog/blog/templates/blog/index.html
Normal file
27
myblog/blog/templates/blog/index.html
Normal 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
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'
|
||||
]
|
||||
|
||||
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",
|
||||
]
|
||||
|
||||
@@ -15,8 +15,9 @@ 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),
|
||||
path('', include('blog.urls')),
|
||||
]
|
||||
|
||||
Reference in New Issue
Block a user