在 Django 中启用 sluggified URL
我正在尝试在 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:
, 'django.views.generic.list_detail.object_detail', info_dict),http://127.0.0.1:8000/articles/1/
or
http://127.0.0.1:8000/articles/1/another-article/
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在问这个问题之前我应该多一点耐心,因为我找到了答案:
第一个模式允许 /articles/1/ 形式的 URL,这意味着第二个 urlpattern(包括 slug)是可选的。
I should have been just a little more patient before asking this question because I figured out the answer:
The first pattern allows URLs of the form /articles/1/ which means that the second urlpattern (to include the slug) is optional.