fix view publist_date

This commit is contained in:
2025-07-26 20:15:13 +08:00
parent cb64aa0911
commit 67bc42e862
4 changed files with 25 additions and 3 deletions

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

View File

@@ -1,4 +1,5 @@
from django.db import models
from django.utils import timezone
# Create your models here.
@@ -7,6 +8,8 @@ class Post(models.Model):
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 self.title
return f"{self.title} ({self.publish_date.strftime('%Y-%m-%d')})"

View File

@@ -9,7 +9,7 @@
<div style="max-width: 800px; margin: 0 auto; padding: 0 20px; text-align: center;">
<!-- 添加网站标题 -->
<h1>{{ post.title }}</h1>
<p>{{ post.created_at }}</p>
<p>{{ post.publish_date }}</p>
<div>{{ post.content|linebreaks }}</div>
<p><a href="/">← 返回首页</a></p>
</div>

View File

@@ -12,7 +12,7 @@
{% 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" }}
发布时间:{{ post.publish_date|date:"Y年n月j日 H:i" }}
</li>
{% endfor %}
</ul>