找不到Django 404页面blog.views.post_detail

时间:2018-07-29 08:55:32

标签: python django python-3.x web

我是django的新手,我使用mysql。我按照教程制作了django博客。我创建了列表和详细信息博客,但是单击博客的详细信息帖子时,出现错误,找不到页面(404)。提出者:blog.views.post_detail。这些是我的网站urls.py,博客urls.py,models.py和views.py。

网站urls.py:

from django.conf.urls import include, url
from django.contrib import admin

urlpatterns = [
    url(r'^admin/', include(admin.site.urls)),
    url(r'^blog/', include('blog.urls',
                            namespace='blog',
                            app_name='blog')),
] 

博客urls.py:

from django.conf.urls import url,include
from . import views

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

models.py:

# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.db import models
from django.utils import timezone
from django.contrib.auth.models import User
from django.core.urlresolvers import reverse

class PublishedManager(models.Manager):
    def get_queryset(self):
        return super(PublishedManager,self).get_queryset()\
            .filter(status='published')
class Post(models.Model):
    STATUS_CHOICES = (('draft', 'Draft'), ('published', 'Published'),)
    title = models.CharField(max_length=250)
    slug = models.SlugField(max_length=250,unique_for_date='publish')
    author = models.ForeignKey(User,related_name='blog_posts')
    body = models.TextField()
    publish = models.DateTimeField(default=timezone.now)
    created = models.DateTimeField(auto_now_add=True)
    updated = models.DateTimeField(auto_now=True)
    status = models.CharField(max_length=10,choices=STATUS_CHOICES,default='draft')
    objects = models.Manager()
    published = PublishedManager()

    class Meta:
        ordering=('-publish',)

    def __str__(self):
        return self.title

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

views.py:

# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.shortcuts import render,get_object_or_404
from .models import Post
from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger
from django.views.generic import ListView
def post_list(request):
    object_list = Post.published.all()
    paginator = Paginator(object_list,3)
    page = request.GET.get('page')
    try:
        posts = paginator.page(page)
    except PageNotAnInteger:
        posts = paginator.page(1)
    except EmptyPage:
        posts = paginator.page(paginator.num_pages)
    return render(request,'blog/post/list.html',{'page':page,'posts':posts})

class PostListView(ListView):
    queryset = Post.published.all()
    context_object_name = 'posts'
    paginate_by = 3
    template_name = 'blog/post/list.html'

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

1 个答案:

答案 0 :(得分:1)

我认为也许您应该使用“ pk”作为参数,而不要使用复杂的网址

def post_detail(request, pk):
    post = get_object_or_404(Post,pk=pk)
    return render (request,
                'blog/post/detail.html',
                {'post':post})

urlpatterns = [
    url(r'^$', views.PostListView.as_view(),name='post_list'),
    url(r'^(?P<pk>[0-9]+)/$',
        views.post_detail,
        name='post_detail'),
        ]

不要让没有人喜欢的事情变得复杂

如果您仍然希望使用它,请尝试从一开始就使其变得超级简单,然后添加您的内容以查看真正出问题的地方。

从一开始使用id,然后尝试添加一年,然后是一个月,然后再添加其他内容。您会发现问题所在