Search code examples
pythondjangodjango-urls

Confused with django urls


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?


Solution

  • 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.