WAMP服务器在DEBUG = True时加载静态文件,而在DEBUG = False时加载静态文件

时间:2019-05-16 10:50:06

标签: python django django-staticfiles django-settings

我使用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>

1 个答案:

答案 0 :(得分:0)

我通过在\文件中使用反斜杠/正斜杠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>