2016-04-18 3 views
2

djangoを使用してブログを構築していました。djangoで定義されたURLでオブジェクトを取得できません

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

とはget_absolute_url機能::
私の記事のモデルは、発行日付を表示フィールドを含めるこれは私のURLである

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

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

これは、記事を表示私の見解でありますブログのプロジェクトで:

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

、プロジェクト内の記事アプリのURLを:

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

私の期待される結果は次のようである:URLは記事が/article/2016/04/18/first-article/であるとき、それはスラグ初段と特定の日付に掲載された記事を表示します。代わりに表示されます:

指定されたクエリに一致する記事はありません。

私は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のバグはありますか? 私は数日間この問題に取り組み、意見やヒントをいただきました。前もって感謝します。

答えて

0

この記事のフラグはstatus = 'published'に設定されていますか?

+0

はい、私は 'status =' published''を使ってテストを投稿しました。 –

1

USE_TZ = False 

Hereがオリジナルの答えで、settings.pyにはFalseにUSE_TZ値を変更してください。

関連する問題