【发布时间】:2020-10-22 20:28:54
【问题描述】:
Django 3.0.7
urls.py
urlpatterns = [
path(
'<slug:categories>/',
include(('categories.urls', "categories"), namespace="categories")
),
]
categories/urls.py
urlpatterns = [
path('', CategoryView.as_view(), name='list'),
re_path(r'.+', include(('posts.urls.post', "posts"), namespace="posts")),
]
这是我编写正则表达式以捕获不少于一个任意符号的错误尝试。
posts/urls/post.py
urlpatterns = [
path('draft/<slug:slug>/', PostDetailView.as_view(), name="draft_detail"),
path('<slug:slug>/', PostDetailView.as_view(), name="detail"),
]
第一个问题
当我加载 http://localhost:8000/linux/install-os-ubuntu/ 时,我得到了这个:
Page not found (404)
Request Method: GET
Request URL: http://localhost:8000/linux/install-os-ubuntu/
Using the URLconf defined in pcask.urls, Django tried these URL patterns, in this order:
[name='home']
^admin/
polls/
applications/
draft/authors/
authors/
email/
facts/
<slug:categories>/ [name='list']
<slug:categories>/ .+ draft/<slug:slug>/ [name='draft_detail']
<slug:categories>/ .+ <slug:slug>/ [name='detail']
tags/ [name='tags']
__debug__/
^media/(?P<path>.*)$
^static/(?P<path>.*)$
^static/(?P<path>.*)$
The current path, linux/install-os-ubuntu/, didn't match any of these.
另一个问题
>>> p = Post.objects.first()
>>> p.get_absolute_url()
'/news/.efremov/'
那是url中出现的一个点。
这里发生了什么,我该如何组织:
-
http://localhost:8000/linux/ 路由到 CategoryView。
-
其他任何内容都通过以下网址进入 PostDetailView:
- http://localhost:8000/linux/draft/install-os-ubuntu/
- http://localhost:8000/linux/install-os-ubuntu/
【问题讨论】:
-
在我的架构中,任何帖子都有一个类别。
标签: django django-urls