Django: Views and URLs Configured, but Pages Not Found (404 Error) - only /admin shows

50 Views Asked by At

I'm encountering an issue with my Django project. I've set up views and URLs for several pages like 'landing', 'chat', 'impressum', 'sales', 'team', and 'user'. However, when I try to access these pages in the browser, I receive a 404 error indicating that the pages cannot be found. Here's the relevant code I've implemented:

from django.contrib import admin
from django.urls import path
from . import views

urlpatterns = [
    path('admin/', admin.site.urls),
    path('', views.landing, name='landing'),
    path('chat/', views.chat, name='chat'),
    path('impressum/', views.impressum, name='impressum'),
    path('sales/', views.sales, name='sales'),
    path('team/', views.team, name='team'),
    path('user/', views.user, name='user'),
]

# views.py
from django.shortcuts import render

def landing(request):
    return render(request, 'landing.html')
def chat(request):
    return render(request, 'chat.html')
def impressum(request):
    return render(request, 'impressum.html')
def sales(request):
    return render(request, 'sales.html')
def team(request):
    return render(request, 'team.html')
def user(request):
    return render(request, 'user.html')

# settings.py
import os
TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [os.path.join(BASE_DIR, 'teacherassistant/templates')],
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
            ],
        },
    },
]

Despite correctly defining URLs and views, I'm unable to render the respective HTML templates for each page as intended. It seems like there's an issue with how Django is handling the routing.

enter image description here

1

There are 1 best solutions below

0
user16171413 On

Your app name appears to be teacherassistant. First, make sure this is included in your INSTALLED_APPS list in your settings.py.

Then open your project level urls.py which is automatically created in your project directory django_teacherassistant\urls.py and enter the following code:

from django.contrib import admin
from django.urls import path, include

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

Then in your app directory i.e. teacherassistant, create an app urls.py file and enter the following code:

from django.urls import path

from . import views

app_name = 'teacherassistant'
urlpatterns = [

    path('', views.landing, name='landing'),
    path('chat/', views.chat, name='chat'),
    path('impressum/', views.impressum, name='impressum'),
    path('sales/', views.sales, name='sales'),
    path('team/', views.team, name='team'),
    path('user/', views.user, name='user'),

]

Note: you have to create your app urls.py file but the project urls.py file is automatically created for you. Please do not mix up the two! See Django URLs Path.