即使那个帖子存在,我的视图也会给出404错误

时间:2015-03-12 00:36:55

标签: python django

在此之前没有发生但是在安装django-contrib-comments应用程序之后,当我点击帖子详细信息链接获取帖子时,我收到404错误。但是在shell中没有问题

my urls.py:

urlpatterns = patterns('',
url(r'^$', views.index, name='index'),
url(r'^(?P<type>\w+)/(?P<slug>[\w|\W]+)/$', views.included_posts,
                                            name="included_posts"),
url(r'^post/(?P<slug>[\w|\W]+)/$', views.detail, name="detail"),
url(r'^paginated-tags/$', views.listing, name="listing"),
)    

my views.py:

def detail(request, slug):
    posts = Post.published_posts.all()
    post = get_object_or_404(posts, slug=slug)
    return render(request, 'blog/index.html', {'post': post})   

published_posts 是我的自定义管理员。

1 个答案:

答案 0 :(得分:0)

您的included_posts网址会在post/someslug网址之前抓取detail网址。

included_posts移到urls.py

的末尾
urlpatterns = patterns('',
    url(r'^$', views.index, name='index'),
    url(r'^post/(?P<slug>[\w|\W]+)/$', views.detail, name="detail"),
    url(r'^paginated-tags/$', views.listing, name="listing"),
    url(r'^(?P<type>\w+)/(?P<slug>[\w|\W]+)/$', views.included_posts,
                                                name="included_posts"),
)

旁注:([\w-]+)是slugs的常见正则表达式。您的([\w|\W]+)将匹配任何字符串(例如:"some () nonslug [] chars"