使用 apache 服务器 (WAMP) 在 windows 10 上部署 django web 应用程序不显示管理站点的 CSS

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

On Windows 10 django app when 运行 using development server by using command python manage.py 运行server shows admin login page and admin site correctly .但是如果使用 apache 服务时相同路径的同一个 django 应用程序没有正确显示 css 管理员登录和其他管理站点页面。

我已尝试按照 https://docs.djangoproject.com/en/3.2/howto/deployment/wsgi/modwsgi/ 中的步骤操作,但有些 configuration/setting 是 missed/incorrect,导致 css 未应用于管理员登录页面和管理站点。 我的开发和部署是在装有 WAMP 服务器 3.2.3.3 的 windows 10 台计算机上进行的。 我已经安装了 mod_wsgi,以下是 settings.py 的相关部分 导入 os 从路径库导入路径

# 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'

我的项目urls.py 如下所示

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如下

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

以下行已添加到 apache 服务器httpd.conf

# 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>

以下为httpd-vhosts.conf文件内容

# 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服务器安装在C:\wamp64,C:\wamp64\www内容为空。 虚拟环境是使用 virtualenvwrapper-win 创建的,它的名字是 hellodjangodeployapacheEnv.

当 django webpapp 运行 使用 python manage.py 运行 服务器并使用 http://127.0.0.1:8000 打开时,它正确显示管理员登录 page/admin 站点但它是为 apache 提供上述配置设置 http://localhost/admin 显示管理员登录页面和没有 css 的管理站点。 上述设置/配置文件需要进行哪些更改,以便在 windows 10.?

上使用 apache 服务器提供服务时,管理员登录/管理站点显示正常 CSS

在您的 Apache 配置中,静态别名必须指向您的 static_root 文件夹 因此它看起来像

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