簡體   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