Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Collectstatic only for certain apps

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?

like image 693
Carlos Mayo Avatar asked Nov 07 '15 16:11

Carlos Mayo


People also ask

How does Django Collectstatic work?

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.

What does manage PY Collectstatic do?

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.

Can Django serve static files in production?

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.

What is Staticfiles_dirs?

STATICFILES_DIRS is the list of folders where Django will search for additional static files aside from the static folder of each app installed.


1 Answers

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.

like image 140
Louis Barranqueiro Avatar answered Sep 21 '22 19:09

Louis Barranqueiro