Django Admin Page not found (404) - First time accessing. error with only admin page
TL;DR: Having trouble accessing 0.0.0.0/8000/admin but with other pages (e.g. http://127.0.0.1:8000/about/) everything is OK.
I'm creating a Django project, but I'm not reaching admin page. Other pages are OK.
Error:
Page not found (404)
Request Method:GET Request URL:http://127.0.0.1:8000/admin
Using the URLconf defined in
ProjetoDjango131026.urls, Django tried these URL patterns, in this order:
[name='index']
contact/ [name='contact']
about/ [name='contact']The current path,
admin, didn’t match any of these.You’re seeing this error because you have
DEBUG = Truein your Django settings file. Change that toFalse, and Django will display a standard 404 page.
urls.py:
"""
URL configuration for ProjetoDjango131026 project.
The `urlpatterns` list routes URLs to views. For more information please see:
https://docs.djangoproject.com/en/5.2/topics/http/urls/
Examples:
Function views
1. Add an import: from my_app import views
2. Add a URL to urlpatterns: path('', views.home, name='home')
Class-based views
1. Add an import: from other_app.views import Home
2. Add a URL to urlpatterns: path('', Home.as_view(), name='home')
Including another URLconf
1. Import the include() function: from django.urls import include, path
2. Add a URL to urlpatterns: path('blog/', include('blog.urls'))
"""
from django.contrib import admin
from django.urls import path, include
from django.conf import settings
from ProjetoDjango131026.views import index, contact, about # added in lesson 25
""" urlpatterns = [
path('admin/', admin.site.urls),
path('', index, name='index'), # added in lesson 25
path('contact/', contact, name='contact'), # added in lesson 25
path('about/', about, name='contact'), # added in lesson 25
] # added in lesson 25 """
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('ProjetoDjango131026.urls')), #altered in lesson 25
path('contact/', include('ProjetoDjango131026.urls')), # added in lesson 25
path('about/', include('ProjetoDjango131026.urls')), # added in lesson 25
]
App urls.py:
from django.urls import path # File created in lesson 25
from ProjetoDjango131026.views import index, contact, about, admin # added in lesson 25
urlpatterns = [
path('', index, name='index'), # added in lesson 25
path('contact/', contact, name='contact'), # added in lesson 25
path('about/', about, name='contact'), # added in lesson 25
]
I think your project is a little bit messed up. I advice you to start from Django official tutorial. It'll let you understand how a basic Django project is structured https://www.djangoproject.com/start/
There is also a framework for starting up a new fresh Django project with recommended Django style and Django best practice. take a look: https://cookiecutter-django.readthedocs.io/en/latest/
Back on the question, probably you have something like this in settings.py:
ROOT_URLCONF = 'ProjetoDjango131026.urls'
Django doesn't search directly for an url.py file but it reads the setting ROOT_URLCONF in settings.py .
ProjetoDjango131026 is a Python module path so when the server starts the inner urls.py is loaded:
from django.urls import path # File created in lesson 25
from ProjetoDjango131026.views import index, contact, about, admin # added in lesson 25
urlpatterns = [
path('', index, name='index'), # added in lesson 25
path('contact/', contact, name='contact'), # added in lesson 25
path('about/', about, name='contact'), # added in lesson 25
]
For this reason, admin is not found. You can fix in a very simple but bad way by adding:
path('admin/', admin.site.urls)
To the inner urls.py(the one you called app.py).
The other option is to change ROOT_URLCONF to:
ROOT_URLCONF = 'urls'
so Django will loads top level urls.py (not the app one) which will include the inner urls.py (the app one).
I think you should fix your project structure before going on. Watch again the video you are talking about, check for some steps you miss.
If you need more help there is a Discord community for Django: https://discord.com/invite/xcRH6mN4fa
Good luck!