小能豆

django url 混淆

python

我试图在 Django 应用程序中定义路由,但它一直向我显示这样的错误:-

Using the URLconf defined in gptclone.urls, Django tried these URL patterns, in this order:

chat [name='home']
about [name='about']
api [name='chatAPI']
The empty path didn’t match any of these

虽然我已经在我的应用程序 urls.py 中定义了这样的路由:

from django.contrib import admin

from django.urls import path

from home.views import home, about, chatAPI

urlpatterns = [

    path('chat',home,name='home'),

    path('about', about, name = 'about'),

    path('api', chatAPI, name ='chatAPI'),

]

在我的项目 urls.py 中,我已经有了:

from django.contrib import admin

from django.urls import path, include

urlpatterns = [ 



  path('', include('home.urls'))

]

你能告诉我我在这里做错了什么吗?


阅读 88

收藏
2023-05-21

共1个答案

小能豆

您还没有指向基本 URL 的路径 + 视图!

这是你目前所拥有的:

[
  # myproject.url
  path('', include('home.urls'))   # include, NOT an actual view

    # myapp.url (included)

    path('chat',home,name='home'),
    # localhost:8000/chat

    path('about', about, name = 'about'),
    # localhost:8000/about

    path('api', chatAPI, name ='chatAPI'),
    # localhost:8000/api
]

因此,如果您想要一个 localhost:8000/ 的视图,您只需在您的应用程序 urls.py 中添加一个,例如:

[
  path('',home,name='home'),
  # localhost:8000/

  path('about', about, name = 'about'),
  # localhost:8000/about

  # etc
]

我通常会这样想这些include陈述,prepend blah to all of these urls 所以也许这样想会有所帮助。

2023-05-21