我創建了一個新的 django 應用程式并使用
python manage.py runserver. 它運行正常,我可以看到默認頁面

隨著外殼輸出:
Django version 4.0.1, using settings 'storefront.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CTRL-BREAK.
[23/Jan/2022 18:52:07] ←[m"GET / HTTP/1.1" 200 10697←[0m
[23/Jan/2022 18:52:07] ←[36m"GET /static/admin/css/fonts.css HTTP/1.1" 304 0←[0m
[23/Jan/2022 18:52:07] ←[36m"GET /static/admin/fonts/Roboto-Bold-webfont.woff HTTP/1.1" 304 0←[0m
[23/Jan/2022 18:52:07] ←[36m"GET /static/admin/fonts/Roboto-Regular-webfont.woff HTTP/1.1" 304 0←[0m
[23/Jan/2022 18:52:07] ←[36m"GET /static/admin/fonts/Roboto-Light-webfont.woff HTTP/1.1" 304 0←[0m
Not Found: /favicon.ico
[23/Jan/2022 18:52:07,704] - Broken pipe from ('127.0.0.1', 56807)
然后我添加了一個新應用程式,如下所示:
python manage.py startapp playground
然后我添加了一個動作處理程式
游樂場/views.py
def say_hello(request):
return HttpResponse('Hello World!')
我還在檔案夾中添加urls.py了playground
游樂場/urls.py
urlpatterns = [
path('hello/', views.say_hello)
]
urls.py我補充的主要內容:
根/urls.py
urlpatterns = [
path('admin/', admin.site.urls),
path('playground/', include('playground.urls')),
]
在主要的 settings.py 中,我包含playground在INSTALLED_APPS串列中。然后我運行了應用程式,我可以確認該路徑127.0.0.1:800/playground/hello按預期作業。
但是,空路徑127.0.0.1:8000/不再有效。我收到此錯誤:

空路徑僅在我評論playground網址時才有效,
urlpatterns = [
path('admin/', admin.site.urls),
# once the next line is commented, the empty path works fine
# path('playground/', include('playground.urls')),
]
這是請求空路徑后日志顯示的內容:
Django version 4.0.1, using settings 'storefront.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CTRL-BREAK.
Not Found: /
[23/Jan/2022 18:47:36] ←[33m"GET / HTTP/1.1" 404 2174←[0m
Not Found: /favicon.ico
[23/Jan/2022 18:47:36,801] - Broken pipe from ('127.0.0.1', 56786)
我究竟做錯了什么?
uj5u.com熱心網友回復:
您沒有做錯任何事,默認的 django 頁面僅在您沒有任何其他 url 模式可以嘗試時顯示(Github)
現在您需要添加一個路徑,/或者如果您愿意,您可以直接呼叫 django路徑
from django.views.debug import default_urlconf
path('', default_urlconf)
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/420188.html
標籤:
