获取错误:
NoReverseMatch:反向'single_view'带参数'('article',2013,12,12,'triumphmotorcycleslaunched10newbikesinindia',16)'和关键字参数'{}'未找到。尝试过1种模式:[u'(P [article | review | blog | news | video])/(P \ d {4})/(P \ d {2})/(P \ d {2} )/(P \ W +)/(P \ d +)$']
这是我的URLConf,模型和视图
/urls.py
...
url(r'^(P<post_type>[article|review|blog|news|video])/', include('post.urls', namespace='post_views')),
...
/post/urls.py
urlpatterns = patterns('',
url(r'^$', PostView.as_view(), name="index"),
url(r'^(P<year>\d{4})/(P<month>\d{2})/(P<day>\d{2})/(P<slug>\w+)/(P<post_id>\d+)$', PostView.single, name="single_view")
)
和 get_absolute_url models.Post class
/post/models.py
def get_absolute_url(self):
from django.core.urlresolvers import reverse
return reverse('post_views:single_view', args=['article', 2013, 12, 12, str(self.slug).replace('-', ''), self.id])
并查看
/post/views.py
def single(self, request, post_type, year, month, day, slug, post_id, *args, **kwargs):
post_single = Post.objects.get(pk=1)
context = {
"post_single": post_single,
}
return self.render_to_response(context);
解析程序错误:在@Yeo建议后尝试
from django.core.urlresolvers import reverse, resolve
from post.models import Post
p = Post.objects.get(pk=16)
reverse('post_views:single_view', args=['article', 2013, 12, 12, p.slug, p.id])
resolve('/article/2013/12/12/triumphlaunched10newbikesinindia/16/')
Resolver404:{u'path':u'article / 2013/12/12 / triumphlaunched10newbikesinindia / 16 /',u'tried':[[],[(无:车辆)^车辆/&gt;],[ (无:post_views)^(P [文章|评论|博客|新闻|视频])/&gt;],[(无:图库)^图库/&gt;],[(无:作者)^作者/&gt;] ,[。*)$&gt;],[(无:无)^ admin / doc /&gt;],[(管理员:管理员)^ admin /&gt;],[(无:无)^ admin_tools /&gt;] ]}
答案 0 :(得分:1)
您的正则表达式格式错误,请尝试替换
r'(P<post_type>[article|review|blog|news|video])'
带
r'(?P<post_type>article|review|blog|news|video)'
注意queston标志和没有方括号。
相似,替换
r'^(P<year>\d{4})/(P<month>\d{2})/(P<day>\d{2})/(P<slug>\w+)/(P<post_id>\d+)$
与
r'^(?P<year>\d{4})/(?P<month>\d{2})/(?P<day>\d{2})/(?P<slug>\w+)/(?P<post_id>\d+)$