在 django 1.3 中使用和引用 slugfield 的正确方法是什么
在 django 1.3 中使用和引用 slugfield 的正确方法是什么,
例如以下代码应通过 slug 链接到通用视图,但是收到 NoReverseMatch 错误。
Caught NoReverseMatch while rendering: Reverse for 'single_post' with arguments '('', u'post-2')' and keyword arguments '{}' not found.
根据我的理解,错误在于模板,但是作为新手并且在 {% url single_post slug=post.slug %} 上尝试了许多不同的变体,情况可能并非如此。
有人可以解释一下为什么会发生这种情况,以便我了解问题所在以及如何解决。
我尝试过 {% url single_post slug=post.slug %},{% url single_post slug %}{% url single_post slug=post.slug %} 和许多其他变体
非常感谢所有帮助
模型
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"),
模板
{% url single_post slug post.slug %}
Whats the correct way to use and refer to a slugfield in a django 1.3
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.
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 tried {% url single_post slug=post.slug %},{% url single_post slug %}{% url single_post slug=post.slug %} and many other variations
All help is greatly appreciated
model
slug = models.SlugField(max_length=120, unique=True)
url
url(r'^post/(?P<slug>[a-z-]+)/
template
{% url single_post slug post.slug %}
, list_detail.object_detail,
{'queryset': Post.objects.all(), 'template_object_name': 'post', 'slug_field': 'slug'}, name="single_post"),
template
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您的正则表达式不允许使用数值。尝试:
Your regex doesn't allow for numeric values. Try:
在您的模板中,假设
post
是您模型的一个实例:您的 url 正则表达式应如下所示:
要测试上述正则表达式,请尝试使用各种有效的内容直接在浏览器中访问一些帖子蛞蝓,看看它们是否有效。完成此操作后,开始测试 url 名称。
In your template, assuming
post
is an instance of your model:Your url regex should look like the following:
To test the above regex, try to access a few posts directly in your browser with a variety of valid slugs and see if they works. Once this is done, start testing the url names.
slug 值可以包含任何
az
、AZ
、0-9
、_
和-.前 3 个由特殊字符
w
表示,由于-
本身就是一个特殊字符,因此我们需要使用反斜杠\
来表示它们>。所以正确的表达变成至少这对我来说是有效的。
A slug value can contain any
a-z
,A-Z
,0-9
,_
and-
. The first 3 are represented by the special characterw
and since-
itself is a special character, we need to use represent them both using a backslash\
. So the correct expression becomesAt least this is what is working in my case.
在 Django 1.5 中,slug 验证器使用此正则表达式:
您可以在 早期版本是
)[-\w]+
但我猜在 Python3 中 \w 匹配非 ascii 字符,例如变音符号。请参阅 https://github.com/django/django/blob/stable/1.5.x/django/core/validators.py#L106
urls.py 中使用此正则表达式:
您可以在 早期版本是
[-\w]+
但我猜在 Python3 中 \w 匹配非 ascii 字符,例如变音符号。In Django 1.5 the slug validator uses this regex:
In earlier versions it was
)[-\w]+
but I guess in Python3 \w matches non ascii characters like umlauts.See https://github.com/django/django/blob/stable/1.5.x/django/core/validators.py#L106
You can use this regex in urls.py:
In earlier versions it was
[-\w]+
but I guess in Python3 \w matches non ascii characters like umlauts.