I manage all my static files with Grunt in my Django project. I have setup a gruntfile for my project that gets all js and css files and then concats and minifies them into one file. Those files are copied to a directory named /static/source
and in my settings I have configured static files like this:
STATICFILES_DIRS = (
os.path.join(PROJECT_ROOT, 'static/build'),
)
STATIC_ROOT = os.path.join(PUBLIC_ROOT, 'static')
STATICFILES_FINDERS = (
'django.contrib.staticfiles.finders.FileSystemFinder',
)
All this works fine and I don't have any problem with this. The problem is when I try to use the admin
app for example. If I add to STATICFILES_FINDERS
the django.contrib.staticfiles.finders.AppDirectoriesFinder
, when I do the collectatic process, it also copies all static files from my apps (the source files which I have concatenated and minified with Grunt).
Is there any way to collect the static files only for a certain app?
collectstatic. Collects the static files into STATIC_ROOT . Duplicate file names are by default resolved in a similar way to how template resolution works: the file that is first found in one of the specified locations will be used. If you're confused, the findstatic command can help show you which files are found.
Using the collectstatic command, Django looks for all static files in your apps and collects them wherever you told it to, i.e. the STATIC_ROOT . In our case, we are telling Django that when we run python manage.py collectstatic , gather all static files into a folder called staticfiles in our project root directory.
Static Files in Development Mode During development, as long as you have DEBUG set to TRUE and you're using the staticfiles app, you can serve up static files using Django's development server. You don't even need to run the collecstatic command.
STATICFILES_DIRS is the list of folders where Django will search for additional static files aside from the static folder of each app installed.
You can use the i
flag of collectstatic
command to ignore staticfiles from apps.
E.g: python manage.py collectstatic -i app -i an_other_app
If you use grunt, you can use grunt-exec
plugin to run this command automatically.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With