英文:
Confused with django urls
问题
I was trying to define routes in django app but it keeps showing me error like this:
在尝试在Django应用程序中定义路由时,但它一直显示以下错误:
However I have already defined routes in my app urls.py like this:
但是,我已经在我的应用程序的urls.py中定义了路由,就像这样:
In my projects urls.py I already have:
在我的项目的urls.py中,我已经有了:
Can you please tell me what I am doing wrong here?
你能告诉我我在这里做错了什么吗?
英文:
I was trying to define routes in django app but it keeps showing me error like this:
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
However I have already defined routes in my app urls.py like this:
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'),
]
In my projects urls.py I already have:
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('', include('home.urls'))
]
Can you please tell me what I am doing wrong here?
答案1
得分: 1
You don't have a Path + View to the base url yet!
This is what you've currently got:
[
# 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
]
So if you wanted a View for localhost:8000/ you'd just have to add one in your app urls.py
Something like:
[
path('',home,name='home'),
# localhost:8000/
path('about', about, name = 'about'),
# localhost:8000/about
# etc
]
I usually think of those include
statements like, prepend blah to all of these urls
so maybe thinking of it like that will help.
英文:
You don't have a Path + View to the base url yet!
This is what you've currently got:
[
# 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
]
So if you wanted a View for localhost:8000/ you'd just have add one in your app urls.py
Something like:
[
path('',home,name='home'),
# localhost:8000/
path('about', about, name = 'about'),
# localhost:8000/about
# etc
]
I usually think of those include
statements like, prepend blah to all of these urls
so maybe thinking of it like that will help.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论