在此之前没有发生但是在安装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 是我的自定义管理员。
答案 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"
)