Confused with django urls

huangapple go评论87阅读模式
英文:

Confused with django urls

问题

I was trying to define routes in django app but it keeps showing me error like this:

  1. 在尝试在Django应用程序中定义路由时但它一直显示以下错误

However I have already defined routes in my app urls.py like this:

  1. 但是我已经在我的应用程序的urls.py中定义了路由就像这样

In my projects urls.py I already have:

  1. 在我的项目的urls.py我已经有了

Can you please tell me what I am doing wrong here?

  1. 你能告诉我我在这里做错了什么吗
英文:

I was trying to define routes in django app but it keeps showing me error like this:

  1. Using the URLconf defined in gptclone.urls, Django tried these URL patterns, in this order:
  2. chat [name='home']
  3. about [name='about']
  4. api [name='chatAPI']
  5. The empty path didnt match any of these

However I have already defined routes in my app urls.py like this:

  1. from django.contrib import admin
  2. from django.urls import path
  3. from home.views import home, about, chatAPI
  4. urlpatterns = [
  5. path('chat',home,name='home'),
  6. path('about', about, name = 'about'),
  7. path('api', chatAPI, name ='chatAPI'),
  8. ]

In my projects urls.py I already have:

  1. from django.contrib import admin
  2. from django.urls import path, include
  3. urlpatterns = [
  4. path('', include('home.urls'))
  5. ]

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:

  1. [
  2. # myproject.url
  3. path('', include('home.urls')) # include, NOT an actual view
  4. # myapp.url (included)
  5. path('chat',home,name='home'),
  6. # localhost:8000/chat
  7. path('about', about, name = 'about'),
  8. # localhost:8000/about
  9. path('api', chatAPI, name ='chatAPI'),
  10. # localhost:8000/api
  11. ]

So if you wanted a View for localhost:8000/ you'd just have to add one in your app urls.py
Something like:

  1. [
  2. path('',home,name='home'),
  3. # localhost:8000/
  4. path('about', about, name = 'about'),
  5. # localhost:8000/about
  6. # etc
  7. ]

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:

  1. [
  2. # myproject.url
  3. path('', include('home.urls')) # include, NOT an actual view
  4. # myapp.url (included)
  5. path('chat',home,name='home'),
  6. # localhost:8000/chat
  7. path('about', about, name = 'about'),
  8. # localhost:8000/about
  9. path('api', chatAPI, name ='chatAPI'),
  10. # localhost:8000/api
  11. ]

So if you wanted a View for localhost:8000/ you'd just have add one in your app urls.py
Something like:

  1. [
  2. path('',home,name='home'),
  3. # localhost:8000/
  4. path('about', about, name = 'about'),
  5. # localhost:8000/about
  6. # etc
  7. ]

I usually think of those include statements like, prepend blah to all of these urls
so maybe thinking of it like that will help.

huangapple
  • 本文由 发表于 2023年5月21日 07:33:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/76297733.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定