collectstatic only for certain apps

  • Last Update :
  • Techknowledgy :

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',
)

Suggestion : 2

On subsequent collectstatic runs (if STATIC_ROOT isn’t empty), files are copied only if they have a modified timestamp greater than the timestamp of the file in STATIC_ROOT. Therefore if you remove an application from INSTALLED_APPS, it’s a good idea to use the collectstatic --clear option in order to remove stale static files.,django.contrib.staticfiles collects static files from each of your applications (and any other places you specify) into a single location that can easily be served in production.,For an introduction to the static files app and some usage examples, see How to manage static files (e.g. images, JavaScript, CSS). For guidelines on deploying static files, see How to deploy static files.,Overrides the core runserver command if the staticfiles app is installed and adds automatic serving of static files. File serving doesn’t run through MIDDLEWARE.

from django.contrib.staticfiles
import storage

class MyStaticFilesStorage(storage.StaticFilesStorage):
   def __init__(self, * args, ** kwargs):
   kwargs['file_permissions_mode'] = 0o640
kwargs['directory_permissions_mode'] = 0o760
super().__init__( * args, ** kwargs)
$ python manage.py collectstatic--help
...\ > py manage.py collectstatic--help
from django.contrib.staticfiles.apps
import StaticFilesConfig

class MyStaticFilesConfig(StaticFilesConfig):
   ignore_patterns = [...] # your custom ignore list
$ python manage.py findstatic css / base.css admin / js / core.js
Found 'css/base.css'
here:
   /home/special.polls.com / core / static / css / base.css /
   home / polls.com / core / static / css / base.css
Found 'admin/js/core.js'
here:
   /home/polls.com / src / django / contrib / admin / media / js / core.js
...\ > py manage.py findstatic css\ base.css admin\ js\ core.js
Found 'css/base.css'
here:
   /home/special.polls.com / core / static / css / base.css /
   home / polls.com / core / static / css / base.css
Found 'admin/js/core.js'
here:
   /home/polls.com / src / django / contrib / admin / media / js / core.js

Suggestion : 3

Collects the static files from all installed apps and copies them to the STATICFILES_STORAGE.,You can limit the apps parsed by providing a list of app names:,Overrides the core runserver command if the staticfiles app is installed (in INSTALLED_APPS) and adds automatic serving of static files and the following new options.,Duplicate file names are resolved in a similar way to how template resolution works. Files are initially searched for in STATICFILES_DIRS locations, followed by apps in the order specified by the INSTALLED_APPS setting.

$ python manage.py collectstatic--exclude - dirs admin polls
$ python manage.py collectstatic--help
$ python manage.py findstatic css / base.css--first /
   home / special.polls.com / core / media / css / base.css
django - admin.py runserver--nostatic
django - admin.py runserver--insecure

Suggestion : 4

Last updated July 28, 2022

If collectstatic failed during a build, a traceback was provided that will be helpful in diagnosing the problem. If you need additional information about the environment collectstatic was run in, use the DEBUG_COLLECTSTATIC configuration.

$ heroku config: set DEBUG_COLLECTSTATIC = 1

Sometimes, you may not want Heroku to run collectstatic on your behalf. You can disable the collectstatic build step with the DISABLE_COLLECTSTATIC configuration:

$ heroku config: set DISABLE_COLLECTSTATIC = 1

Suggestion : 5

This is a Django app that provides helpers for serving static files.,A Django app that provides helpers for serving static files.,For small projects, this isn’t a big deal, because you can just keep the media somewhere your web server can find it. However, in bigger projects – especially those comprised of multiple apps – dealing with the multiple sets of static files provided by each application starts to get tricky.,The main website for django-staticfiles is github.com/jezdez/django-staticfiles where you can also file tickets.

Use your favorite Python packaging tool to install staticfiles from PyPI, e.g.:

pip install django - staticfiles

Added "staticfiles" to your INSTALLED_APPS setting:

INSTALLED_APPS = [
   #...
   "staticfiles",
]

Set your STATIC_URL setting to the URL that handles serving static files:

STATIC_URL = "/static/"

In development mode (when DEBUG = True) the runserver command will automatically serve static files:

python manage.py runserver

Once you are ready to deploy all static files of your site in a central directory (STATIC_ROOT) to be served by a real webserver (e.g. Apache, Cherokee, Lighttpd, Nginx etc.), use the collectstatic management command:

python manage.py collectstatic

(optional) In case you use Django’s admin app, make sure the ADMIN_MEDIA_PREFIX setting is set correctly to a subpath of STATIC_URL:

ADMIN_MEDIA_PREFIX = STATIC_URL + "admin/"

Suggestion : 6

In this tutorial, we will look at some common mistakes that are often made by Django developers and ways to avoid them. This tutorial is useful even if you’re a skilled Django developer because these mistakes aren’t just limited to new developers taking their first stab at Django. ,We can do it simply by using ManifestStaticFilesStorage as STATICFILES_STORAGE (be careful, hashing is only enabled in DEBUG=false mode) and running the collectstatic management command discussed above. This will decrease assets requests count to your production website and will make your website render much faster.,In this tutorial, we will look at some common mistakes that are often made by Django developers and ways to avoid them. This tutorial is useful even if you’re a skilled Django developer because mistakes, like maintaining an unmanageably large settings or naming conflicts in static assets, aren’t just limited to new developers taking their first stab at Django.,This mistake is usually made by new Python and Django developers that don’t know about Python’s environment isolation features.

Check out the minimal configuration example:

from split_settings.tools
import optional, include

include(
      'components/base.py',
      'components/database.py',
      'components/*.py',

      # the project different envs settings optional('envs/devel/*.py'),
      optional('envs/production/*.py'),
      optional('envs/staging/*.py'),

      #
      for any local settings optional(‘local_settings.py '),)

Here is example structure for a portal application which has a lot of resources and Python modules.

root @c5b96c395cfb: /test# tree project/apps / portal /
   project / apps / portal / ├──__init__.py├── admin.py├── apps.py├── management│├── __init__.py│└── commands│├── __init__.py│└── update_portal_feeds.py├── migrations│└── __init__.py├── models.py├── static│└── portal│├── css│├── img│└── js├── templates│└── portal│└── index.html├── templatetags│├── __init__.py│└── portal.py├── tests.py├── urls.py└── views.py

11 directories, 14 files

You’ll end up with a project structure like this:

root @c5b96c395cfb: /test# tree -L 3
   .├──deploy│├── chef│└── docker│├── devel│└── production├── docs├── logs├── manage.py├── media├── project│├── __init__.py│├── apps││├── auth││├── blog││├── faq││├── pages││├── portal││└── users│├── conf│├── settings.py│├── static│├── templates│├── urls.py│└── wsgi.py└── static└── admin├── css├── fonts├── img└── js

25 directories, 5 files