Confused with django urls

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

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.

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:

确定