Compare commits
10 Commits
1c7d752a57
...
010b45c328
| Author | SHA256 | Date | |
|---|---|---|---|
| 010b45c328 | |||
| 3f9847d6fb | |||
| a8acfaf5b5 | |||
| 7bcc6460c4 | |||
| 67bc42e862 | |||
| cb64aa0911 | |||
| dbc3dac253 | |||
| 5a585b4fc9 | |||
| 99cb1d7170 | |||
| 0cb2b6e621 |
@@ -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)
|
||||
|
||||
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)),
|
||||
],
|
||||
),
|
||||
]
|
||||
19
myblog/blog/migrations/0002_post_publish_date.py
Normal file
19
myblog/blog/migrations/0002_post_publish_date.py
Normal file
@@ -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),
|
||||
),
|
||||
]
|
||||
@@ -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))
|
||||
|
||||
21
myblog/blog/templates/blog/detail.html
Normal file
21
myblog/blog/templates/blog/detail.html
Normal file
@@ -0,0 +1,21 @@
|
||||
<!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;">
|
||||
<h1 style="text-align: center;">{{ post.title }}</h1>
|
||||
<p>发布时间:{{ post.publish_date|date:"Y年n月j日 H:i" }}</p>
|
||||
<!-- 使用get_markdown_content方法渲染Markdown内容 -->
|
||||
<div>{{ post.get_markdown_content|safe }}</div>
|
||||
<br>
|
||||
<a href="{% url 'index' %}">返回首页</a>
|
||||
</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.publish_date|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',
|
||||
'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",
|
||||
]
|
||||
|
||||
@@ -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')),
|
||||
]
|
||||
|
||||
41
myblog/requirements.txt
Normal file
41
myblog/requirements.txt
Normal file
@@ -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
|
||||
Reference in New Issue
Block a user