问题描述
使用正确的方法并指向django中的slugfield 1.3
Whats the correct way to use and refer to a slugfield in a django 1.3
例如,以下代码应通过slug链接到通用视图,但NoReverseMatch错误被收到。
for example the following code should link via slug to a generic view however the NoReverseMatch error is received.
Caught NoReverseMatch while rendering: Reverse for 'single_post' with arguments '('', u'post-2')' and keyword arguments '{}' not found.
从我的理解这个说法,错误在于模板,但是作为新手,尝试了许多不同{%url single_post slug = post.slug%}的变化可能不是这种情况。
From my understanding this saying that the error lies in the template however being a newbie and having tried many different variations on {% url single_post slug=post.slug %} this may not be the case.
有人可以解释为什么会发生这种情况,以便我可以了解问题出在,并且要修复。
Could someone please explain why this is happening so that I can understand where the problem lies andhow to fix.
Ive尝试{%url single_post slug = post.slug%},{%url single_post slug%} {%url single_post slug = post。 slug%}和许多其他变体
Ive tried {% url single_post slug=post.slug %},{% url single_post slug %}{% url single_post slug=post.slug %} and many other variations
非常感谢所有帮助
模型
slug = models.SlugField(max_length=120, unique=True)
url
url(r'^post/(?P<slug>[a-z-]+)/$', list_detail.object_detail,
{'queryset': Post.objects.all(), 'template_object_name': 'post', 'slug_field': 'slug'}, name="single_post"),
e
{% url single_post slug post.slug %}
推荐答案
您的正则表达式不允许数值。尝试:
Your regex doesn't allow for numeric values. Try:
(?P<slug>[\w-]+)
这篇关于什么是正确的使用方式,并指向django中的slugfield 1.3的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!