Justin543543
Justin543543

Reputation: 9

Django not recognizing app’s url and views. It keeps raising a 404 `Response`

Hi I am trying to create my first Django project and having issues getting an app to work inside my Django project. I simply am trying to map the url code 8000/home1/hello/ to display the basic text "hello world" but failing to do so. I keep getting this error code:

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

admin/ The current path, home1/hello, didn’t match any of these."

Here is what I have done:

  1. created a django project named web3
  2. created an app called home1 and added a urls.py file to this app
  3. updated the settings.py folder INSTALLE_APPS to have 'home1.apps.Home1Config',
  4. home1 > urls.py includes this code:

from django.urls import path from . import views

urlpatterns = [
    path('hello/', views.hello_world, name="hello_world"),
]

  1. home1>views.py includes this code:

from django.shortcuts import render
from django.http import HttpResponse

def hello_world(request):
    return HttpResponse("you finally did it justin")

6.web3> urls.py includes this code:

from django.contrib import admin
from django.urls import path, include
from home1.views import hello_world  # Import the hello_world view function


urlpatterns = [
    path('admin/', admin.site.urls),
    path('', include('home1.urls')),
]

  1. I am pretty confident my file outlay is correct as I used the basic code to create the files (django-admin startproject web3) and (python manage.py startapp home1)

I am fully lost on this one... any help would be greatly appreciate!!!

Upvotes: 0

Views: 495

Answers (2)

Kiarash Gh
Kiarash Gh

Reputation: 390

the main issue is that the URL pattern home1/hello/ is not matching because the web3/urls.py is including home.urls at the root URL path this means home1/hello/ will actually be matched as hello/ from the root

to fix this you should do these steps:

  • make sure your project structure looks like this:
web3/
    manage.py
    web3/
        __init__.py
        settings.py
        urls.py
        wsgi.py
    home1/
        __init__.py
        admin.py
        apps.py
        models.py
        tests.py
        views.py
        urls.py
        migrations/
            __init__.py

  • make sure home1 is added to INSTALLED_APPS
  • your home1/urls.py should looks like this :
from django.urls import path
from . import views

urlpatterns = [
    path('hello/', views.hello_world, name='hello_world'),
]
  • include home1.urls in web3/urls.py and use home1/ as prefix:
from django.contrib import admin
from django.urls import path, include

urlpatterns = [
    path('admin/', admin.site.urls),
    path('home1/', include('home1.urls')),  
]

and finally your home1/views.py should looks like this:

from django.shortcuts import render
from django.http import HttpResponse

def hello_world(request):
    return HttpResponse("you finally did it justin")

other things that comes to my mind:

  • look for any typos in your file names, function names and configuration
  • restart the server to ensure all changes are loaded
  • run django's built-in system check to identify any issues: python manage.py check
  • sometimes browsers cache old responses so clearing the cache or trying an incognito window might help
  • use django's url resolver to verify that url pattern is correctly configured add this code to one of your views temporarily to debug:
from django.urls import resolve
print(resolve('/home1/hello/'))

I hope this helps you.

Upvotes: 0

Chukwujiobi Canon
Chukwujiobi Canon

Reputation: 4091

The path "home1/hello/" does not exist on your Server. Only "hello/" and "admin/" exists.

It is a common misconception that if you include a URLconf into your root URLconf like so:

urlpatterns = [
    #•••Rest of code•••
    path('', include('home1.urls')),
]

That all routes from that app will begin with the name of the app —e.g. "home1" in the above.

This is not true. For there to exist a basename, you must give a basename.

urlpatterns = [
    #•••Rest of code•••
    path('home1/', include('home1.urls')),
]

Now all routes from that app will begin with "home1".

Upvotes: 0

Related Questions