Django Admin Panel and Sub URLs Returning 404 Error on Deployment

32 views Asked by At

have recently deployed my Django project to a server, and while the index URL (mannyebi.com) loads correctly along with static files, accessing sub URLs such as mannyebi.com/blogs or even the admin panel (mannyebi.com/admin) results in a 404 error.

Locally, the project runs smoothly, indicating the issue may stem from the deployment configuration. I've ensured that all necessary URL patterns are defined in the urls.py files and that Django's admin app is properly installed and configured.

However, despite these measures, I'm still encountering difficulties accessing sub URLs beyond the homepage. Is there a specific configuration step or server setting that I might be overlooking to resolve this 404 error and enable access to sub URLs on my deployment? Any insights or troubleshooting suggestions would be greatly appreciated.

I deployed my Django project to a server. Upon navigating to sub URLs such as mannyebi.com/blogs or mannyebi.com/admin, I encountered a 404 error.

I verified that all necessary URL patterns were defined in urls.py and that Django's admin app was properly configured. I expected these URLs to load correctly, similar to how they did on my local development environment. However, despite these efforts, the 404 errors persisted.

I'm seeking guidance on resolving this issue and enabling access to sub URLs on the deployment.

settings.py :

"""
Django settings for imazh project.

Generated by 'django-admin startproject' using Django 4.2.5.

For more information on this file, see
https://docs.djangoproject.com/en/4.2/topics/settings/

For the full list of settings and their values, see
https://docs.djangoproject.com/en/4.2/ref/settings/
"""

from pathlib import Path
import os

# Build paths inside the project like this: BASE_DIR / 'subdir'.
BASE_DIR = Path(__file__).resolve().parent.parent

# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/4.2/howto/deployment/checklist/

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = os.environ.get('SECRET_KEY', 'django-insecure-)!ly+xdx%u1es*og=g!l^ldj(t)1b%bvr6u7ld6%f46-g7vrxl')


# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = SECRET_KEY = os.environ.get('DEBUG', False)

#if you wanna use docker use this :
#ALLOWED_HOSTS = os.environ.get("ALLOWED_HOSTS", "127.0.0.1").split(',')

#if you use windows redis server use this :
ALLOWED_HOSTS = ['mannyebi.com', "www.mannyebi.com"]

TIME_FORMAT = 'h:i A'

# Application definition

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',

    'django.contrib.humanize',

    'django_jalali',

    'base.apps.BaseConfig',
    'panel.apps.PanelConfig',
    
    'django_celery_beat',
]


LANGUAGE_CODE = 'fa'

import locale
locale.setlocale(locale.LC_ALL, "fa_IR.utf8")


MIDDLEWARE = [
    'django.middleware.security.SecurityMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
]

ROOT_URLCONF = 'imazh.urls'

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [
            BASE_DIR / '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',
            ],
        },
    },
]

WSGI_APPLICATION = 'imazh.wsgi.application'


# Database
# https://docs.djangoproject.com/en/4.2/ref/settings/#databases

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': BASE_DIR / 'db.sqlite3',
    }
}


# Password validation
# https://docs.djangoproject.com/en/4.2/ref/settings/#auth-password-validators

AUTH_PASSWORD_VALIDATORS = [
    {
        'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
    },
]

AUTH_USER_MODEL = 'base.User'


# Internationalization
# https://docs.djangoproject.com/en/4.2/topics/i18n/

LANGUAGE_CODE = 'en-us'

TIME_ZONE = 'Iran'

USE_I18N = True

USE_TZ = False


# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/4.2/howto/static-files/

STATIC_URL = '/static/'
MEDIA_URL = '/images/'


STATICFILES_DIRS = [
    BASE_DIR  / 'static'
]

MEDIA_ROOT = os.path.join(BASE_DIR, "static/images")
STATIC_ROOT = '../Imazh/statics/'


# Default primary key field type
# https://docs.djangoproject.com/en/4.2/ref/settings/#default-auto-field

DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'

#use this if you wanna use docker :
#CELERY_BROKER_URL = os.environ.get('CELERY_BROKER', 'redis://redis:6379/0')
#CELERY_RESULT_BACKEND = os.environ.get('CELERY_BACKEND', 'redis://redis:6379/0')

#else use this :
CELERY_BROKER_URL = "redis://localhost:6379/0"
CELERY_RESULT_BACKEND = "redis://localhost:6379/0"


SESSION_EXPIRE_AT_BROWSER_CLOSE = True

by changing the debug to False I get this error on my index page :

Incomplete response received from application

project(Imazh) urls.py :

from django.contrib import admin
from django.urls import path, include
from django.conf import settings
from django.conf.urls.static import static

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

base(application) urls.py:

from .views import indexPage, blogs, BlogPage, VirtualTour, Booking, bookingSubmit, Authcheck, verify_signedup_user, create_new_user, academy
from django.urls import path



urlpatterns = [
    path('', indexPage, name='indexPage'),
    path('blogs/', blogs, name='blogs'),
    path('BlogPage/<str:pk>', BlogPage, name='BlogPage'),
    path('Virtual Tour/', VirtualTour, name='VirtualTour'),
    path('booking/', Booking, name='booking'),
    path('bookingsubmit/', bookingSubmit, name='bookingSubmit'),
    path('Authcheck/', Authcheck, name='Authcheck'),
    path('verify user/', verify_signedup_user, name='verifysuser'),
    path('create user/', create_new_user, name='createuser'),
    path('Imazh academy/', academy, name='academy'),

]

panel(application) urls.py :

from django.urls import path
from .views import Users,get_free_time_ajax,send_sms_to_users,admins,admins,deactiveDays, addUsers,history_appointments,academy,User_Edit, adminDash,UpdateAppointments, expired_Appointments, bannUsers,cancelAppointment, deleteUsers, logoutUser, active_Appointments, DeleteAppointment, AddAppointment, blogs, deleteBlog, UpdateBlogs, Users_detail, userDash, UpdateAppointmentuser


urlpatterns = [
    path('Admin Dashboard/', adminDash, name='adminDash'),
    path('active appointments/', active_Appointments, name='Appointments'),
    path('expired appointments/', expired_Appointments, name='expAppointments'),
    path('Users/', Users, name='users'),
    path('User /<str:pk>', Users_detail, name='Users_detail'),
    path('User edit/<str:pk>', User_Edit, name='User_edit'),
    path('Blogs/', blogs, name='panelblogs'),
    path('Admins/', admins, name='Admins'),
    path('deactive Days/', deactiveDays, name='deactiveDays'),#type:ignore

    path('Delete Appointment/<str:pk>', DeleteAppointment, name='DeleteAppointment'),
    path('Add Appointment/', AddAppointment, name='AddAppointment'),

    path('get_free_times/', get_free_time_ajax, name='get_free_times'),
    path('send_sms_to_users/<str:pk>', send_sms_to_users, name='send_sms_to_users'),
    
    path('logoutUser/', logoutUser, name='Logout'),
    path('add User/', addUsers, name='addUsers'),
    path('bann User/<int:pk>', bannUsers, name="bannUser"),
    path('delete User/<int:pk>', deleteUsers, name="deleteUsers"),
    path('delete Blog/<int:pk>', deleteBlog, name="deleteBlog"),
    path('update Blog/<int:pk>', UpdateBlogs, name="UpdateBlogs"),
    path('update appointments/<int:pk>', UpdateAppointments, name="UpdateAppointments"),


    path('User Dashboard/<str:pk>', userDash, name='userDash'),
    path('Update Appointment/<str:pk>', UpdateAppointmentuser, name='UpdateAppointmentuser'),
    path('delete Appointment/<str:pk>', cancelAppointment, name='cancelAppointment'),
    path('history Appointments/<str:pk>', history_appointments, name='history_appointments'),
    path('Academy/<str:pk>', academy, name='Academy'),
]

python version on server : 3.11.5

Django version on server : 5.0

views.py for blogs page :

def blogs(request):
    if request.user.is_authenticated:
        user_requested = Profile.objects.get(user=request.user)
    else:
        user_requested = None
    blogs = Weblog.objects.all().order_by('-created')


    context = {"blogs":blogs, 'user':user_requested}
    return render(request, 'base/blogs.html', context)

urls.py for blogs : from .views import blogs

urlpatterns = [

path('blogs/', blogs, name='blogs'),

]

error for blogs url

0

There are 0 answers