Search code examples
pythondjangowindowsapachewamp

django web app deployment on windows 10 using apache server(WAMP) not displaying CSS for admin site


On Windows 10 django app when run using development server by using command python manage.py runserver shows admin login page and admin site correctly. but if same django app at same path when served using apache is not displaying css correctly for admin login and other admin site pages.

I have tried to follow steps at https://docs.djangoproject.com/en/3.2/howto/deployment/wsgi/modwsgi/ but some configuration/setting is missed/incorrect resulting in css not applied to admin login page and admin site. my development and deployment is on same windows 10 computer with WAMP server 3.2.3.3. I have installed mod_wsgi and following are relevent section from settings.py import os from pathlib import Path

# Build paths inside the project like this: BASE_DIR / 'subdir'.


BASE_DIR = Path(__file__).resolve().parent.parent




# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = 'secrete key is listed here'

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = False

ALLOWED_HOSTS = ['*']

MEDIA_URL = '/media/'

MEDIA_ROOT = os.path.join(BASE_DIR, 'media')

STATICFILES_DIRS = [os.path.join(BASE_DIR, 'static'), ]

STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')

STATICFILES_FINDERS = [
    "django.contrib.staticfiles.finders.FileSystemFinder",
    "django.contrib.staticfiles.finders.AppDirectoriesFinder",
]


# Application definition

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

    'polls.apps.PollsConfig',
]

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 = 'hellodjangodeployapache_project.urls'

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [os.path.join(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 = 'hellodjangodeployapache_project.wsgi.application'


# Database


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


# Password validation


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',
    },
]

SITE_ID = 1
# Internationalization


LANGUAGE_CODE = 'en-us'

TIME_ZONE = 'UTC'

USE_I18N = True

USE_L10N = True

USE_TZ = True


# Static files (CSS, JavaScript, Images)


STATIC_URL = '/static/'

# Default primary key field type

    enter code here

DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'

my projects urls.py looks like as below

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

urlpatterns = [
                  path('polls/', include('polls.urls')),
                  path('admin/', admin.site.urls),
                  path('', views.home, name='home'),

              ] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

wsgi.py is as below

import os

from django.core.wsgi import get_wsgi_application

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'hellodjangodeployapache_project.settings')

application = get_wsgi_application()

django project directory structure is as shown in picture below

project directory structure

following lines are added to httpd.conf of apache server

# LoadFile "d:/pythoninstallation/python39.dll"
LoadModule wsgi_module "c:/users/admin/envs/hellodjangodeployapacheenv/lib/site-packages/mod_wsgi/server/mod_wsgi.cp39-win_amd64.pyd"
WSGIPythonHome "c:/users/admin/envs/hellodjangodeployapacheenv"


WSGIPythonPath "D:/djangoprojects/hellodjangodeployapache/hellodjangodeployapache_project"

<Directory "D:/djangoprojects/hellodjangodeployapache/hellodjangodeployapache_project">
<Files wsgi.py>
Require all granted
</Files>
</Directory>

Alias /media/ "D:/djangoprojects/hellodjangodeployapache/hellodjangodeployapache_project/media/"

Alias /static/ "D:/djangoprojects/hellodjangodeployapache/hellodjangodeployapache_project/static/"

<Directory "D:/djangoprojects/hellodjangodeployapache/hellodjangodeployapache_project/static/">
Require all granted
</Directory>

<Directory "D:/djangoprojects/hellodjangodeployapache/hellodjangodeployapache_project/media/">
Require all granted
</Directory>

WSGIScriptAlias / "D:/djangoprojects/hellodjangodeployapache/hellodjangodeployapache_project/hellodjangodeployapache_project/wsgi.py"

<Directory "D:/djangoprojects/hellodjangodeployapache/hellodjangodeployapache_project/hellodjangodeployapache_project">
<Files wsgi.py>
Require all granted
</Files>
</Directory>

following is httpd-vhosts.conf file content

# Virtual Hosts
#
<VirtualHost *:80>
  ServerName localhost
  ServerAlias localhost
  DocumentRoot "${INSTALL_DIR}/www"
#  <Directory "${INSTALL_DIR}/www/">
#    Options +Indexes +Includes +FollowSymLinks +MultiViews
#    AllowOverride All
#    Require local
#  </Directory>
</VirtualHost>

wamp server is installed at C:\wamp64 and content of C:\wamp64\www is empty. virtual envirornment is created using virtualenvwrapper-win and its name is hellodjangodeployapacheEnv.

when django webpapp is run using python manage.py runserver and opened using http://127.0.0.1:8000 it shows admin login page/admin site properly but it is served apache with above mentioned configuration settings http://localhost/admin shows admin login page and admin site without css. what is change is needed in above mentioned settings/ configuration files so that admin login/ admin site shows normal CSS when served using apache server on windows 10.?


Solution

  • in your Apache config, the static alias must point towards your static_root folder hence it will look like

    Alias /static/ "D:/djangoprojects/hellodjangodeployapache/hellodjangodeployapache_project/staticfiles/"