在 Django 中启用 sluggified URL

发布于 2024-08-09 09:54:42 字数 1284 浏览 7 评论 0原文

我正在尝试在 Django 中启用 SO 使用的形式的 sluggified URL:example.com/id/slug。 我启用 slugs 没有问题,并且当前设置的 URL 格式为: http://127.0.0.1 :8000/articles/id/ (例如/articles/1/),效果很好。相应的 URLpattern 是:

(r'^(?P<object_id>\d+)/$',  'django.views.generic.list_detail.object_detail', info_dict),

如果我将 URL 模式更改为:

(r'^(?P<slug>\d+)/$',  'django.views.generic.list_detail.object_detail', info_dict),

那么当我导航到 http://127.0.0.1:8000/articles/another-article/

当前 URL、articles/another-article/ 与其中任何一个都不匹配。

但是,如果我尝试:

http://127.0.0.1:8000/articles/1/

我收到错误:

未找到与查询匹配的文章

最终我希望能够通过以下任一方式导航到文章:

http://127.0.0.1:8000/articles/1/ 或者 http://127.0.0.1:8000/articles/1/another-article/

I am trying to enable sluggified URLs in Django of the form that SO uses: example.com/id/slug.
I have no problem enabling slugs, and have URLs currently set up of the form: http://127.0.0.1:8000/articles/id/ (eg. /articles/1/) and that works fine. The corresponding URLpattern is:

(r'^(?P<object_id>\d+)/

If I change the URL pattern to:

(r'^(?P<slug>\d+)/

Then I recieve the following eror when I navigate to http://127.0.0.1:8000/articles/another-article/:

The current URL, articles/another-article/, didn't match any of these.

If, however, I try:

http://127.0.0.1:8000/articles/1/

I get the error:

No article found matching the query

Ultimately I want to be able to navigate to an aricle via either:

http://127.0.0.1:8000/articles/1/
or
http://127.0.0.1:8000/articles/1/another-article/

, 'django.views.generic.list_detail.object_detail', info_dict),

If I change the URL pattern to:


Then I recieve the following eror when I navigate to http://127.0.0.1:8000/articles/another-article/:

The current URL, articles/another-article/, didn't match any of these.

If, however, I try:


I get the error:

No article found matching the query

Ultimately I want to be able to navigate to an aricle via either:

http://127.0.0.1:8000/articles/1/
or
http://127.0.0.1:8000/articles/1/another-article/

, 'django.views.generic.list_detail.object_detail', info_dict),

Then I recieve the following eror when I navigate to http://127.0.0.1:8000/articles/another-article/:

The current URL, articles/another-article/, didn't match any of these.

If, however, I try:

I get the error:

No article found matching the query

Ultimately I want to be able to navigate to an aricle via either:

http://127.0.0.1:8000/articles/1/
or
http://127.0.0.1:8000/articles/1/another-article/

, 'django.views.generic.list_detail.object_detail', info_dict),

If I change the URL pattern to:

Then I recieve the following eror when I navigate to http://127.0.0.1:8000/articles/another-article/:

The current URL, articles/another-article/, didn't match any of these.

If, however, I try:

I get the error:

No article found matching the query

Ultimately I want to be able to navigate to an aricle via either:

http://127.0.0.1:8000/articles/1/
or
http://127.0.0.1:8000/articles/1/another-article/

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

枯叶蝶 2024-08-16 09:54:42

在问这个问题之前我应该​​多一点耐心,因为我找到了答案:

(r'^(?P<object_id>\d+)/

第一个模式允许 /articles/1/ 形式的 URL,这意味着第二个 urlpattern(包括 slug)是可选的。

, 'django.views.generic.list_detail.object_detail', info_dict), (r'^(?P<object_id>\d+)/(?P<slug>[-\w]+)/

第一个模式允许 /articles/1/ 形式的 URL,这意味着第二个 urlpattern(包括 slug)是可选的。

, 'django.views.generic.list_detail.object_detail', info_dict),

第一个模式允许 /articles/1/ 形式的 URL,这意味着第二个 urlpattern(包括 slug)是可选的。

I should have been just a little more patient before asking this question because I figured out the answer:

(r'^(?P<object_id>\d+)/

The first pattern allows URLs of the form /articles/1/ which means that the second urlpattern (to include the slug) is optional.

, 'django.views.generic.list_detail.object_detail', info_dict), (r'^(?P<object_id>\d+)/(?P<slug>[-\w]+)/

The first pattern allows URLs of the form /articles/1/ which means that the second urlpattern (to include the slug) is optional.

, 'django.views.generic.list_detail.object_detail', info_dict),

The first pattern allows URLs of the form /articles/1/ which means that the second urlpattern (to include the slug) is optional.

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