1

我对 django 很陌生,并且一直在解决我的大部分问题,直到这个问题。我认为 TimeZone 和 UTC 存在问题,但老实说,我不知道如何解决这个问题。

我正在建立一个博客,并试图查询特定日期以显示帖子。我的问题是我不断得到第二天的值。我什至注意到我看到这个月因为一天而关闭。这是我的详细信息和示例。

我的数据库中有 5 个帖子。当我搜索 9/4 时,我得到 9/3 的数据。9/3 数据返回 9/2 等。因此,当我在 9 月搜索所有帖子时,我也得到了 8/31。

任何帮助将不胜感激。还请解释我为什么会遇到这个问题。

博客标题 发布日期

Test BlogPost 5 Sept. 3, 2012, 5:26 p.m. 
Test BlogPost 4 Sept. 3, 2012, 5:22 p.m.
Test BlogPost 3 Sept. 2, 2012, 7:09 p.m.
Test BlogPost 2 Sept. 1, 2012, 5:29 p.m.
Test BlogPost   Sept. 1, 2012, 5:26 p.m.
Test BlogPost 0 Aug. 31, 2012, 8:18 p.m.
Test BlogPost 00    Aug. 29, 2012, 8:19 p.m.

模型.py

from django.db import models
from django.template.defaultfilters import slugify
import datetime
class BlogPost(models.Model):
  blog_title = models.CharField(max_length=200)
  blog_slug = models.SlugField(unique_for_date='blog_pub_date', help_text='Automatically built from the title.', max_length=200)
  blog_content = models.TextField()
  blog_pub_date = models.DateTimeField('Date published')
  def __unicode__(self):
    return '%s' % self.blog_title
  class Meta:
    ordering = ["-blog_pub_date"]

网址.py

url(r'^blog/(?P<year>\d{4})/$', 'dev_website.views.blog_view_post_year', name='blog_view_post_year'),
url(r'^blog/(?P<year>\d{4})/(?P<month>\d{1,2})/$', 'dev_website.views.blog_view_post_month', name='blog_view_post_month'),
url(r'^blog/(?P<year>\d{4})/(?P<month>\d{1,2})/(?P<day>\d{1,2})/$', 'dev_website.views.blog_view_post_day', name='blog_view_post_day'),
url(r'^blog/(?P<year>\d{4})/(?P<month>\d{1,2})/(?P<day>\d{1,2})/(?P<slug>[0-9A-Za-z-]+)/$', 'dev_website.views.blog_view_post_detail', name='blog_view_post_detail'),

视图.py

def blog_index(request):
  latest_blogpost_list = BlogPost.objects.all().order_by('-blog_pub_date')[:7]
  return render(request, 'blog_index.html', {'latest_blogpost_list': latest_blogpost_list})
def blog_view_post_year(request, year):
  latest_blogpost_list = BlogPost.objects.filter(blog_pub_date__year=int(year)).order_by('-blog_pub_date')[:7]
  return render(request, 'blog_index.html', {'latest_blogpost_list': latest_blogpost_list})
def blog_view_post_month(request, year, month):
  latest_blogpost_list = BlogPost.objects.filter(blog_pub_date__month=int(month)).order_by('-blog_pub_date')[:7]
  return render(request, 'blog_index.html', {'latest_blogpost_list': latest_blogpost_list})
def blog_view_post_day(request, year, month, day):
  latest_blogpost_list = BlogPost.objects.filter(blog_pub_date__day=int(day)).order_by('-blog_pub_date')[:7]
  return render(request, 'blog_index.html', {'latest_blogpost_list': latest_blogpost_list})
def blog_view_post_detail(request, year, month, day, slug):
  #  blogpost_detail = BlogPost.objects.get(blog_pub_date__year=int(year), blog_pub_date__month=int(month), blog_pub_date__day=int(day), blog_slug=slug)
  #  blogpost_detail = BlogPost.objects.get(blog_pub_date__year=int(year), blog_pub_date__month=int(month), blog_slug=slug)
  blogpost_detail = BlogPost.objects.get(blog_pub_date__day=day)
  return render(request, 'blog_view_post.html', {'blogpost': blogpost_detail})

html代码

<h2>BLOG</h2>
{% if latest_blogpost_list %}
  {% for blogpost in latest_blogpost_list %}
    <h3><a href="blog/{{ blogpost.blog_pub_date|date:"Y/m/d" }}/{{ blogpost.blog_slug }}/">{{ blogpost.blog_title }}</a></h3>
    <p>[ {{ blogpost.blog_pub_date }} ]</p>
    <hr>
    <p>{{ blogpost.blog_content }}</p>
    <br />
  {% endfor %}
{% else %}
  <p>No Blog posts are available.</p>
{% endif %}
4

0 回答 0