包括在urlpatterns和404

问题描述 投票:0回答:2

我使用的是 Django 3.0.6.

项目的urls.py

from django.urls import path
from .views import PostDetailView


urlpatterns = [
    path('<int:pk>', PostDetailView.as_view(), name='detail'),
]

项目的urls.py

urlpatterns = []

if DEBUG:
    import debug_toolbar
    urlpatterns = [
        path('__debug__/', include(debug_toolbar.urls)),
    ]
    urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

urlpatterns += [
    path('', HomeView.as_view(), name='home'),
    path('{}'.format("admin/" if DEBUG else "dhjfsljdasdhje32/"), admin.site.urls), # Change admin url for security reasons.
    path('post/', include('post.urls')),
]

浏览器中的URL

http:/localhost:8000post1。

Reslut

Page not found (404)
Request Method: GET
Request URL:    http://localhost:8000/post/1/
Using the URLconf defined in pcask.urls, Django tried these URL patterns, in this order:

__debug__/
^media\/(?P<path>.*)$
[name='home']
admin/
post/ <int:pk> [name='detail']
The current path, post/1/, didn't match any of these.

You're seeing this error because you have DEBUG = True in your Django settings file. Change that to False, and Django will display a standard 404 page.

如何应对这个问题?

django django-urls
2个回答
1
投票

你少了一个 斜杠 在你的URLs配置中。所以,改变你的 post/urls.py

urlpatterns = [
    path('&ltint:pk>/', PostDetailView.as_view(), name='detail'),
                #^^^ trailing slash
]

再试 /post/1/

© www.soinside.com 2019 - 2024. All rights reserved.