无法通过django中定义的url获取对象

时间:2016-04-18 11:08:31

标签: python django datetime django-urls

我正在使用django建立一个博客 我的文章模型包含一个显示发布日期的字段:

publish = models.DateTimeField(default = timezone.now)

和get_absolute_url函数:

def get_absolute_url(self):
    return reverse('article:post_detail', 
                args = [self.publish.year,
                        self.publish.strftime('%m'),
                        self.publish.strftime('%d'),
                        self.slug])

这是显示文章的视图:

def post_detail(request, year, month, day, post):
    post = get_object_or_404(Article, slug = post,
                                  status = 'published',
                                  publish__year = year,
                                  publish__month = month,
                                  publish__day = day)
return render(request, 'post.html', {'post': post})

这是我在博客项目中的网址:

url(r'^article/', include('article.urls', namespace = 'article',app_name = 'article')),

以及项目中文章app的网址:

url(r'^(?P<year>\d{4})/(?P<month>\d{2})/(?P<day>\d{2})/(?P<post>[-\w]+)/$',views.post_detail, name = 'post_detail'),

我的预期结果如下:当文章的网址为/article/2016/04/18/first-article/时,它会显示使用slug第一篇文章在特定日期发布的文章,但显示的是:

  

没有文章与给定的查询匹配。

当我使用python manage.py shell品尝它时,我发现问题似乎与发布字段中的月份和日期有关:

>>> Article.objects.get(pk=1).get_absolute_url()
u'/article/2016/04/18/first-article/'
>>> Article.objects.get(pk=1).publish.year
2016
>>> Article.objects.get(pk=1).publish.month
4
>>> Article.objects.get(pk=1).publish.day
18

但是当我搜索文章时:

>>> from django.shortcuts import get_object_or_404
>>> get_object_or_404(Article, publish__year='2016')
<Article: This is the first article>
>>> get_object_or_404(Article, publish__year='2016', slug='first-article', status='published')
<Article: This is the first article>
>>> get_object_or_404(Article, publish__year='2016', publish__month='04')
Traceback (most recent call last):
File "<console>", line 1, in <module>
File "/media/psf/Home/Porject/env/my_blog_new/lib/python2.7/site-packages/django/shortcuts.py", line 157, in get_object_or_404
  raise Http404('No %s matches the given query.' % queryset.model._meta.object_name)
Http404: No Article matches the given query.
>>> get_object_or_404(Article, publish__year='2016', publish__day='18')
Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "/media/psf/Home/Porject/env/my_blog_new/lib/python2.7/site-packages/django/shortcuts.py", line 157, in get_object_or_404
    raise Http404('No %s matches the given query.' % queryset.model._meta.object_name)
Http404: No Article matches the given query.

似乎任何与月和日相关的事都没有用,但我无法弄明白为什么 我使用的是mysql,虽然我不认为它与这个问题有关,因为我的主页运行正常 是处理datetime字段的mysql的错误吗? 我坚持这个问题好几天了,感谢任何意见和建议。提前谢谢。

3 个答案:

答案 0 :(得分:1)

请在settings.py,

中将USE_TZ值修改为False
x_description

Here是原始答案。

答案 1 :(得分:0)

您确定该文章的标记设置为status = 'published'吗?

答案 2 :(得分:0)

这是在view.py中使用的

    def post_detail(request,year,month,day,post):
         post=get_object_or_404(Post, 
         publish__year=year,publish__month=month,publish__day=day,status='published', slug=post)
         return render(request, 'blog/post/detail.html', {'post': post})