繁体   English   中英

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

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

在 Windows 10 django 应用程序上,通过使用命令 python manage.py runserver 正确显示管理登录页面和管理站点。 但是,如果使用 apache 提供相同路径的相同 django 应用程序,则管理员登录和其他管理站点页面无法正确显示 css。

我已尝试按照https://docs.djangoproject.com/en/3.2/howto/deployment/wsgi/modwsgi/中的步骤操作,但某些配置/设置丢失/不正确导致 css 不适用于管理登录页面和管理站点. 我的开发和部署是在具有 WAMP 服务器 3.2.3.3 的同一台 windows 10 计算机上。 我已经安装了 mod_wsgi,以下是 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'

我的项目 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工程目录结构如下图

项目目录结构

以下行添加到 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。

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没有 css 的管理登录页面和管理站点。 在上述设置/配置文件中需要进行哪些更改,以便管理员登录/管理站点在使用 windows 10 上的 apache 服务器提供服务时显示正常的 CSS。

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM