Is manage.py collectstatic needed on each edit of static file?

trex picture trex · May 20, 2015 · Viewed 7.3k times · Source

I was using django- 1.3.1 by now. Want to give a try to latest version i.e 1.8.1. So, configured a test project. I was unfamiliar how to tackle with static files with django-1.8.1, since explored it more.

I created a /var/www/appmedia/ directory and put my static components in to it. like bootstrap, css, js, images, etc.

settings.py.

"""
Django settings for myapp project.

Generated by 'django-admin startproject' using Django 1.8.1...
"""

# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
import os

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/1.8/howto/deployment/checklist/

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = '+3)e5#343!^(cvy8f9c4hpku*h#$z8*sa)(7a#azv1!!z@d--wsq5s5'

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

ALLOWED_HOSTS = []


# Application definition

INSTALLED_APPS = (
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'myapp.common'
)

MIDDLEWARE_CLASSES = (
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
    'django.middleware.security.SecurityMiddleware',
)

ROOT_URLCONF = 'myapp.urls'

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': ["/home/trex/Work/myapp/myapp/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 = 'myapp.wsgi.application'


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

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql_psycopg2',
        'NAME': "myapp",
        'USER':"postgres", 
        'PASSWORD':"postgres", 
        'HOST':"" 
    }
}


# Internationalization
# https://docs.djangoproject.com/en/1.8/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/1.8/howto/static-files/
STATIC_ROOT = '/home/trex/Work/myapp/myapp/static'
STATIC_URL = '/static/'
STATICFILES_DIRS = (
    os.path.join(BASE_DIR, "static"),
    '/var/www/appmedia/',
)

To collect static I run python manage.py collectstatic which created following

directory structure:

├── myapp
│   ├── common
│   │   ├── migrations
│   │   ├── __pycache__
│   │   └── static
│   ├── __pycache__
│   ├── static
│   │   ├── admin
│   │   ├── bootstrap
│   │   ├── css
│   │   ├── images
│   │   ├── js
│   │   └── scripts
│   └── templates
└── manage.py

urls.py

urlpatterns = [
    url(r'^admin/', include(admin.site.urls)),
    url(r'^$', views.home, name='home'),
]+ static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)

The issue is, I'm unable to edit my static files. Always have to keep it in /var/www/appmedia to edit it, run the collectstatic, then only my changes to static files are reflecting. Did I miss something in configuring static files?

Answer

chaos picture chaos · May 20, 2015

That is because you are pointing your server to

STATIC_ROOT = '/home/trex/Work/myapp/myapp/static'.

When developing and using runserver, running collectstatic is not needed, nor does STATIC_ROOT need to be set. You can simply use the files from your "static" folder that you define at the app level. STATICFILES_DIRS is used only when you have static files that you are using from a different location than a "static" directory in your apps.

Check out the documentation on this with attention to the different ways you serve your files: development vs. production

editing for code addition to urlconf

from django.conf import settings 
from django.contrib.staticfiles.views import serve 
if settings.DEBUG: 
    urlpatterns += [ url(r'^static/(?P<path>.*)$', serve), ]