【发布时间】:2018-08-23 00:57:29
【问题描述】:
我想要一个类似这样的 URL:
/(category)/(post-slug)
在链接上,这是我所拥有的:
{% url blog.category blog.slug %}
对于 url.py:
url(r'^(I DON"T KNOW WHAT TO PUT ON THIS PART TO GET THE CATEGORY)/(?P<slug>[0-9A-Za-z._%+-]+)', views.post, name='post'),
谢谢
编辑: 这就是我现在所拥有的: 在 /
处仍有 NoReverseMatch 错误urls.py
url(r'^(?P<category>[0-9A-Za-z._%+-]+)/(?P<slug>[0-9A-Za-z._%+-]+)$', views.post, name='post'),
index.html
<a href="{% url blog.category blog.slug %}">
views.py
def post(request, slug, category):
try:
blog = Blog.objects.get(slug=slug)
except Blog.DoesNotExist:
raise Http404('This post does not exist')
return render(request, 'parts/post.html', {
'blog': blog,
})
【问题讨论】:
-
如果 URL 是
/(category)/(post-slug),那么{% url category.blog category.slug %}看起来是错误的。不应该是{% url post.category.slug post.slug %}之类的吗? -
抱歉,我刚才打错了,是的,我正在使用 blog.category 和 blog.slug,我的重点是正则表达式