在 django 1.3 中使用和引用 slugfield 的正确方法是什么

发布于 2024-11-02 04:14:20 字数 926 浏览 1 评论 0原文

在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

找个人就嫁了吧 2024-11-09 04:14:20

您的正则表达式不允许使用数值。尝试:

(?P<slug>[\w-]+)

Your regex doesn't allow for numeric values. Try:

(?P<slug>[\w-]+)
烏雲後面有陽光 2024-11-09 04:14:20

在您的模板中,假设 post 是您模型的一个实例:

{% url single_post post.slug %}

您的 url 正则表达式应如下所示:

url(r'^post/(?P<slug>[\w-]+)/

要测试上述正则表达式,请尝试使用各种有效的内容直接在浏览器中访问一些帖子蛞蝓,看看它们是否有效。完成此操作后,开始测试 url 名称。

, ...

要测试上述正则表达式,请尝试使用各种有效的内容直接在浏览器中访问一些帖子蛞蝓,看看它们是否有效。完成此操作后,开始测试 url 名称。

In your template, assuming post is an instance of your model:

{% url single_post post.slug %}

Your url regex should look like the following:

url(r'^post/(?P<slug>[\w-]+)/

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.

, ...

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.

始终不够 2024-11-09 04:14:20

slug 值可以包含任何 azAZ0-9_-.前 3 个由特殊字符 w 表示,由于 - 本身就是一个特殊字符,因此我们需要使用反斜杠 \ 来表示它们>。所以正确的表达变成

url(r'^post/(?P<slug>[\w\-]+)/

至少这对我来说是有效的。

, ...

至少这对我来说是有效的。

A slug value can contain any a-z, A-Z, 0-9, _ and -. The first 3 are represented by the special character w and since - itself is a special character, we need to use represent them both using a backslash \. So the correct expression becomes

url(r'^post/(?P<slug>[\w\-]+)/

At least this is what is working in my case.

, ...

At least this is what is working in my case.

烟火散人牵绊 2024-11-09 04:14:20

在 Django 1.5 中,slug 验证器使用此正则表达式:

slug_re = re.compile(r'^[-a-zA-Z0-9_]+

请参阅 https://github.com/django/django/blob/stable/1.5.x/django/core/validators.py#L106

urls.py 中使用此正则表达式:

url(r'^post/(?P<slug>[-a-zA-Z0-9_]+)/

您可以在 早期版本是 [-\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 字符,例如变音符号。

, ...

您可以在 早期版本是 [-\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:

slug_re = re.compile(r'^[-a-zA-Z0-9_]+

See https://github.com/django/django/blob/stable/1.5.x/django/core/validators.py#L106

You can use this regex in urls.py:

url(r'^post/(?P<slug>[-a-zA-Z0-9_]+)/

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.

, ...

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文