Is manage.py collectstatic required for every edit of a static file?

I've already used django- 1.3.1

. Want to try the latest ie 1.8.1

. So, the test project is set up. I didn't know how to deal with static files with django-1.8.1 since I studied it more.

I created a directory /var/www/appmedia/

and put my static components in it. for example 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 build the static one, I ran the python manage.py collectstatic

one created after

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 problem is, I can't edit my static files. You always need to store it in /var/www/appmedia

, to edit it, run it collectstatic

, then only my changes will be reflected in static files. Am I missing something when setting up static files?

+3


source to share


3 answers


This is because you are pointing your server to

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

      

There is no runserver

launch collectstatic

required during development and use , and no need to install STATIC_ROOT

. You can simply use files from your "static" folder that you define at the application level. STATICFILES_DIRS

only used when you have static files that you use elsewhere than the "static" directory in your applications.



Check out the documentation on the subject, noting the different ways you serve your files: development and production

edit to add code to urlconf

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

      

+7


source


For Django development server, you can add the following to your settings file:

STATICFILES_DIRS = (
    os.path.join(BASE_DIR, "static"),
    'static/',
)

      



This should be good enough and you won't need to run it collectstatic

every time.

For your live server settings, remove this line or set it to None and be sure to add it STATIC_ROOT

.

+1


source


I just went through the same problem and after spending hours I finally found the easiest breakout. The Chaos method didn't work for me, so I went a little deeper and changed the static settings as follows:

STATIC_URL = '/static/'

if DEBUG:
MEDIA_URL = '/media/'
STATIC_ROOT = os.path.join(os.path.dirname(BASE_DIR),'static','static-only')
MEDIA_ROOT = os.path.join(os.path.dirname(BASE_DIR),'static','media')
STATICFILES_DIRS = (os.path.join(os.path.dirname(BASE_DIR),'static'),)

      

After that, I changed the urls.py file, which now has the following addition

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

      

Needless to say, you need to import the settings into your urls.py file

+1


source







All Articles