I An
I An

Reputation: 21

Cannot get object through the defined url in django

I was building a blog using django.
My Article model include a field that display publishing date:

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

and a get_absolute_url function:

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

This is my view displaying articles:

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})

This is my urls in the blog project:

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

and the urls of the article app within the project:

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

My expected results is like: When the url is article is /article/2016/04/18/first-article/, it will display the article published on the specific date with the slug first-article, but instead it displayed:

No article matches the given query.

When I tasted it using python manage.py shell, I found the problem seemed related to the month and day in the publish field:

>>> 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

But when I searched for the article:

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

It seemed that anything relate to the month and day didn't work but I was unable to figure out why.
I was using mysql, though I don't think it is related to this problem because my home page worked fine.
Is is the bug of mysql handling datetime field? I stuck on this problem for several days and appreciated any opinions and tips. Thank you in advance.

Upvotes: 2

Views: 561

Answers (3)

sorwarduet
sorwarduet

Reputation: 1

This is use in 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})

Upvotes: 0

Jay Lim
Jay Lim

Reputation: 420

Please modify USE_TZ value into False in settings.py,

USE_TZ = False

Here is original answer.

Upvotes: 1

David K.
David K.

Reputation: 116

Are you sure the article has the flag set to status = 'published'?

Upvotes: 0

Related Questions