fix view publist_date
This commit is contained in:
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,4 +1,5 @@
|
|||||||
from django.db import models
|
from django.db import models
|
||||||
|
from django.utils import timezone
|
||||||
|
|
||||||
|
|
||||||
# Create your models here.
|
# Create your models here.
|
||||||
@@ -7,6 +8,8 @@ class Post(models.Model):
|
|||||||
content = models.TextField()
|
content = models.TextField()
|
||||||
created_at = models.DateTimeField(auto_now_add=True)
|
created_at = models.DateTimeField(auto_now_add=True)
|
||||||
updated_at = models.DateTimeField(auto_now=True)
|
updated_at = models.DateTimeField(auto_now=True)
|
||||||
|
# 修改publish_date字段,使其在后台可编辑
|
||||||
|
publish_date = models.DateTimeField(default=timezone.now)
|
||||||
|
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
return self.title
|
return f"{self.title} ({self.publish_date.strftime('%Y-%m-%d')})"
|
||||||
@@ -9,7 +9,7 @@
|
|||||||
<div style="max-width: 800px; margin: 0 auto; padding: 0 20px; text-align: center;">
|
<div style="max-width: 800px; margin: 0 auto; padding: 0 20px; text-align: center;">
|
||||||
<!-- 添加网站标题 -->
|
<!-- 添加网站标题 -->
|
||||||
<h1>{{ post.title }}</h1>
|
<h1>{{ post.title }}</h1>
|
||||||
<p>{{ post.created_at }}</p>
|
<p>{{ post.publish_date }}</p>
|
||||||
<div>{{ post.content|linebreaks }}</div>
|
<div>{{ post.content|linebreaks }}</div>
|
||||||
<p><a href="/">← 返回首页</a></p>
|
<p><a href="/">← 返回首页</a></p>
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
@@ -12,7 +12,7 @@
|
|||||||
{% for post in posts %}
|
{% for post in posts %}
|
||||||
<li style="margin: 10px 0; text-align: left;">
|
<li style="margin: 10px 0; text-align: left;">
|
||||||
<a href="{% url 'detail' post.id %}">{{ post.title }}</a> -
|
<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>
|
</li>
|
||||||
{% endfor %}
|
{% endfor %}
|
||||||
</ul>
|
</ul>
|
||||||
|
|||||||
Reference in New Issue
Block a user