WAMP 服务器在 DEBUG = True 时加载静态文件,而不是在 DEBUG=False 时加载静态文件,即使在 collectstatic 之后

WAMP server loading static files when DEBUG = True not when DEBUG=False even after collectstatic

我使用 mod_wsgi 在 WAMP 服务器上部署了一个基于 django 的应用程序,并使用 运行 py manage.py collectstatic 命令为所有静态文件提供服务。我检查过网站运行良好,但静态文件仅在 DEBUG=True 时才运行。但是根据 Django 的 document DEBUG=True 将在开发服务器上为其他服务器提供静态文件,我们应该使用 DEBUG=False.

我还检查了在 collectstatic

之后收集了所有静态文件

我不明白为什么会这样。

下面是settings.py文件

"""
Django settings for olxApp project.

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

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

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

import os

# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))


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

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = ''

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

ALLOWED_HOSTS = ['10.0.100.148','127.0.0.1','192.168.1.8','localhost']


# Application definition

INSTALLED_APPS = [
    'olx.apps.OlxConfig',
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'sorl.thumbnail',
    'bootstrap3',

]

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


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


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

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
    }
}


# Password validation
# https://docs.djangoproject.com/en/2.1/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',
    },
]
#Added by chirag 
#Note: For site normal user 'olx.myLdapAuth.MyLdapAuth' will be used and for admin 'django.contrib.auth.backends.ModelBackend'
#will be used
AUTHENTICATION_BACKENDS = ['olx.myLdapAuth.MyLdapAuth',
                            'django.contrib.auth.backends.ModelBackend']

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

LANGUAGE_CODE = 'en-us'

TIME_ZONE = 'UTC'

USE_I18N = True

USE_L10N = True

USE_TZ = True


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


STATIC_URL = '/static/'
STATICFILES_DIRS = (
    os.path.join(BASE_DIR, 'olxApp/static'),
)
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR,'media')

Urls.py 文件

 from django.contrib import admin
    from django.urls import include,path

    from django.conf.urls.static import static
    from django.conf import settings
    urlpatterns = [

            path('olx/', include('olx.urls')),
            path('admin/', admin.site.urls),

    ]


    urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
    urlpatterns +=static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)

用于加载 Django 应用程序的 Apache httpd.conf 文件的内容

LoadFile "c:/users/chirag.soni/appdata/local/programs/python/python37/python37.dll"
LoadModule wsgi_module "c:/users/chirag.soni/appdata/local/programs/python/python37/lib/site-packages/mod_wsgi/server/mod_wsgi.cp37-win_amd64.pyd"
WSGIPythonHome "c:/users/chirag.soni/appdata/local/programs/python/python37"

WSGIScriptAlias / "C:\Users\chirag.soni\Downloads\olxApp\olxApp\wsgi.py"
WSGIPythonPath "C:\Users\chirag.soni\Downloads\olxApp"

Alias /media/ "C:\Users\chirag.soni\Downloads\olxApp\media\"
Alias /static/ "C:\Users\chirag.soni\Downloads\olxApp\static\"




<Directory "C:\Users\chirag.soni\Downloads\olxApp\media">
Require all granted
</Directory>

<Directory "C:\Users\chirag.soni\Downloads\olxApp\static">
Require all granted
</Directory>

<Directory "C:\Users\chirag.soni\Downloads\olxApp\olxApp">
<Files wsgi.py>
Require all granted
</Files>
</Directory>

我通过在 httpd.conf 文件

中将反斜杠 \ 正斜杠 / 解决了这个问题

所以最终的 httpd.conf 文件应该如下所示:

LoadFile "c:/users/chirag.soni/appdata/local/programs/python/python37/python37.dll"
LoadModule wsgi_module "c:/users/chirag.soni/appdata/local/programs/python/python37/lib/site-packages/mod_wsgi/server/mod_wsgi.cp37-win_amd64.pyd"
WSGIPythonHome "c:/users/chirag.soni/appdata/local/programs/python/python37"

WSGIScriptAlias / "C:/Users/chirag.soni/Downloads/olxApp/olxApp/wsgi.py"
WSGIPythonPath "C:/Users/chirag.soni/Downloads/olxApp"

Alias /media "C:/Users/chirag.soni/Downloads/olxApp/media/"
Alias /static "C:/Users/chirag.soni/Downloads/olxApp/static/"




<Directory "C:/Users/chirag.soni/Downloads/olxApp/media">
Require all granted
</Directory>

<Directory "C:/Users/chirag.soni/Downloads/olxApp/static">
Require all granted
</Directory>

<Directory "C:/Users/chirag.soni/Downloads/olxApp/olxApp">
<Files wsgi.py>
Require all granted
</Files>
</Directory>