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

97 views
Skip to first unread message

chirag soni

unread,
May 16, 2019, 9:17:43 AM5/16/19
to django...@googlegroups.com

I deployed a django based app on WAMP server using mod_wsgi and to serve all the static files I run the `py manage.py collectstatic` command. I checked that the site is serving well but the static files are serving only when DEBUG=True. But as per the Django's document `DEBUG=True` will serve the static files on the development server for other servers we should use `DEBUG=False`.

I also checked that all the static files got collected after `collectstatic`

**I am not understanding why it is happening.**

Below is the settings.py file
-----------------------------------------------------

    """
    Django settings for olxApp project.
    
    Generated by 'django-admin startproject' using Django 2.1.5.
    
    For more information on this file, see
    
    For the full list of settings and their values, see
    """
    
    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
    
    # 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
    
    DATABASES = {
        'default': {
            'ENGINE': 'django.db.backends.sqlite3',
            'NAME': os.path.join(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',
        },
    ]
    #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
    
    LANGUAGE_CODE = 'en-us'
    
    TIME_ZONE = 'UTC'
    
    USE_I18N = True
    
    USE_L10N = True
    
    USE_TZ = True
    
    
    # Static files (CSS, JavaScript, Images)
    
    
    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 file
------------------------------------------------------------------

   

     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)

Content of Apache's httpd.conf file for loading the Django app
--------------------------------------------------------------------------------------------

    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>





 

Mike Dewhirst

unread,
May 16, 2019, 8:36:34 PM5/16/19
to Django users
On 16/05/2019 11:15 pm, chirag soni wrote:
>
> I deployed a django based app on WAMP server using mod_wsgi and to
> serve all the static files I run the `py manage.py collectstatic`
> command. I checked that the site is serving well but the static files
> are serving only when DEBUG=True. But as per the Django's document
> <https://docs.djangoproject.com/en/2.2/howto/static-files/#configuring-static-files>
> `DEBUG=True` will serve the static files on the development server for
> other servers we should use `DEBUG=False`.
>
> I also checked that all the static files got collected after
> `collectstatic`

They will all be collected under a particular directory on the server
file system. Typically the last directory name on that path is ../static
although it could be anything you like.

When DEBUG is False Django assumes it does not need to server static
files because the web server itself is best placed to do that. Why
should Django do anything else other than execute your software? That
would be wasteful of resources. On the other hand, the webserver (Apache
or nginx) is designed specifically to serve static files.

manage.py collectstatic --settings=<project-settings.py> will gather all
your static files from directories with their roots (typically
os.path.join(BASE_DIR, 'static/' and os.path.join(BASE_DIR,
'<app-name>/static/'))  listed in STATICFILES_DIRS and put them into a
tree of directories rooted at STATIC_ROOT as specified in settings.py

Your web server needs to be configured to serve static files.

Here is an Apache config static files snippet which serves static files
from the exact directory specified in STATIC_ROOT in settings.py. I have
configured mine to keep static files from all vhosts in a single root
separated by project name but away from the individual project roots.

    # serve static stuff from here
    <Directory /var/www/static/<project-name-here>/>
        AllowOverride None
        Require all granted
    </Directory>

I have not read your email beyond the line which says you are not
understanding so I apologise if I have led you astray.

hth

Mike
>
> **I am not understanding why it is happening.**
>
> *Below is the settings.py file*
> *-----------------------------------------------------*
> *
> *
> *Urls.py file*
> *------------------------------------------------------------------*
>
>
>      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)
>
> *Content of Apache's httpd.conf file for loading the Django app*
> *--------------------------------------------------------------------------------------------*
>
>     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>
>
>
>
>
>
> --
> You received this message because you are subscribed to the Google
> Groups "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send
> an email to django-users...@googlegroups.com
> <mailto:django-users...@googlegroups.com>.
> To post to this group, send email to django...@googlegroups.com
> <mailto:django...@googlegroups.com>.
> Visit this group at https://groups.google.com/group/django-users.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/CABCRpYML2vOuZ-fjEbr2Lp2UP4XeAqO7M6msyeue1cScnuRKVw%40mail.gmail.com
> <https://groups.google.com/d/msgid/django-users/CABCRpYML2vOuZ-fjEbr2Lp2UP4XeAqO7M6msyeue1cScnuRKVw%40mail.gmail.com?utm_medium=email&utm_source=footer>.
> For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages