Page not found 404 django-python

  • Context: Python 
  • Thread starter Thread starter shivajikobardan
  • Start date Start date
Click For Summary
SUMMARY

The forum discussion addresses a 404 error encountered in a Django application when attempting to access the "about" and "contact" pages. The user has defined URL patterns in both the main project’s urls.py and the todolist_app.urls, but failed to include a direct path for the "contact" URL in the main urls.py. The correct implementation requires adding path('contact/', include('todolist_app.urls')) to the main urls.py to resolve the issue.

PREREQUISITES
  • Django 4.0 or later
  • Understanding of URL routing in Django
  • Familiarity with Django views and templates
  • Basic knowledge of Python programming
NEXT STEPS
  • Implement path('contact/', include('todolist_app.urls')) in taskmate/urls.py
  • Review Django documentation on URL routing and namespaces
  • Learn about Django's include() function for modular URL management
  • Explore debugging techniques for Django applications
USEFUL FOR

Django developers, web application developers, and anyone troubleshooting URL routing issues in Django projects.

shivajikobardan
Messages
637
Reaction score
54
TL;DR
page not found in django, how do I fix this issue?
Goal:

127.0.0.1:8000/about
Should give:
welcome to to about us page

127.0.0.1:8000/contact
Should give:
welcome to contact page

Problem:
Both are giving "page not found at /"
Code:
Page not found (404)

Request Method:    GET

Request URL:    http://127.0.0.1:8000/contact/
Using the URLconf defined in taskmate.urls, Django tried these URL patterns, in this order:
1) admin/

2) task/

3) todolist/

The current path, contact/, didn't match any of these.

You're seeing this error because you have DEBUG = True in your Django settings file. Change that to False, and Django will display a standard 404 page.

This is my folder structure.
1665382902057.png


Currently, my urls.py of taskmate is:

Python:
from django.contrib import admin

from django.urls import path, include
urlpatterns = [

    path('admin/', admin.site.urls),

    path('task/',include('todolist_app.urls')),

    path('todolist/',include('todolist_app.urls')),

]

And for todolist_app, these are my details:

Urls.py:

Python:
from django.urls import path

from todolist_app import views

#from . import views

urlpatterns = [

    path('', views.todolist),

    path('contact', views.contact,name='contact'),

    path('about', views.about, name="about"), #the path can be anything.

]

Views.py:

Python:
from django.shortcuts import render

from django.http import HttpResponse
# Create your views here.

def todolist(request):

    context={'welcome_text':"welcome to todo list app"}

    return render(request, 'html/todolist.html',context)
def contact(request):

    context={'welcome_text':"welcome to contact page"}

    return render(request, 'html/contact.html',context)
def about(request):

    context={'welcome_text':"welcome to to about us page"}

    return render(request, 'html/about.html',context)
I'm suspecting that the problem is in my main project urls.py as I've not included any details about contact and about URLs there. So, I did this there.

Python:
from django.contrib import admin

from django.urls import path, include
urlpatterns = [
    path('admin/', admin.site.urls),

    path('task/',include('todolist_app.urls')),

    path('todolist/',include('todolist_app.urls')),

    path('about/',include('todolist_app.urls')),

]

The website runs but the goal isn't achieved.
It shows what the URLs
http://127.0.0.1:8000/task/
And
http://127.0.0.1:8000/todolist/
Show.
How do I fix this issue?
 
Last edited:
Technology news on Phys.org
shivajikobardan said:
I'm suspecting that the problem is in my main project urls.py as I've not included any details about contact and about URLs there.
You don't need to suspect, you just need to read the error message which tells you that.

shivajikobardan said:
So, I did this there.
[CODE lang=Python title=taskmate/urls.py]from django.contrib import admin
from django.urls import path, include

urlpatterns = [
path('admin/', admin.site.urls),
path('task/',include('todolist_app.urls')),
path('todolist/',include('todolist_app.urls')),
path('about/',include('todolist_app.urls')),
][/CODE]
[See how I tidied that up to remove additional whitespace and added a title to make it clear what file it was? You should do this when posting.]

But that still doesn't have an entry for path('contact/', ...)!

Learn about how nested path routing works here: https://docs.djangoproject.com/en/4.0/topics/http/urls/#url-namespaces
 
It's not working to achieve the goal i posted in question even after adding stuffs in taskmate/urls.py. As I said above.
 
shivajikobardan said:
It's not working to achieve the goal i posted in question even after adding stuffs in taskmate/urls.py. As I said above.
But you did not add the right stuffs in taskmate/urls.py. As I said above.
pbuk said:
But that still doesn't have an entry for path('contact/', ...)!
 
as i said adding entry like i added for "about" there will lead to same page that i get when i hit 127.0.0.1/task and 127.0.0.1/todolist
 

Similar threads

  • · Replies 10 ·
Replies
10
Views
4K
Replies
3
Views
3K
Replies
9
Views
14K
  • · Replies 3 ·
Replies
3
Views
5K
  • Sticky
  • · Replies 2 ·
Replies
2
Views
503K