Django objects.get __day返回错误的日期

时间:2012-09-04 19:07:42

标签: django django-views django-queryset

我是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.

models.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"]

urls.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'),

views.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 %}

0 个答案:

没有答案