Dino Geek, try to help you

How to organize static files in Django?


Django is a popular framework for developers due to its capabilities in building powerful, scalable applications. One key aspect of using Django or any other web application framework effectively is learning how to properly organize static files. By static files, we mean files that are served by the server in the same format that they’re stored. This includes files like JavaScript or CSS that won’t change unless you manually edit them.

To serve static files, Django comes with the django.contrib.staticfiles contribution module. First, you need to add ‘django.contrib.staticfiles’ to your INSTALLED\_APPS setting. Once you’ve done this, you can collect all your static files into a single directory that you’ve specified by running the collectstatic management command, i.e., python manage.py collectstatic.

Now regarding the organization of static files, they should be stored in a file inside each application. This is called namespacing and follows Django’s app-centric design. For instance, if your project contains an application ‘polls’, Django would expect to find the static files for this application under polls/static/. You may structure these files how you please, e.g., polls/static/polls/css or polls/static/polls/js.

It’s also commonplace to use third-party libraries’ static files. This can be accomplished by using a shortcut: STATICFILES_DIRS. It’s a setting that defines additional locations, on top of STATIC_ROOT, where Django could search for static files.

Then, to include a static files in your templates, utilize the static template tag as follows:

{% load static %}

In not an HTML file, you can use this method:

from django.templatetags.static import static
url = static(‘app/example.jpg’)

Some developers may wish to maintain a set of static files, different from those served in development, specifically for production. You can simply create a new directory in your project specified under STATIC\_ROOT where all the static files will be collected to by collectstatic.

Subsequently, you’ll need to have your production web server set up to serve the STATIC_ROOT directory at the STATIC_URL path. This gives the best performance since these files can be served directly by the web server without involving Python.

In a nutshell, organizing static files in Django involves storing them in each application directory, using the static template tag to reference them, and ensuring your production server is set to serve them at the correct URL.

Sources:
1. Django documentation (https://docs.djangoproject.com/en/3.2/howto/static-files/)
2. Django for Beginners by William S. Vincent
3. Django for Professionals by William S. Vincent.


Simply generate articles to optimize your SEO
Simply generate articles to optimize your SEO





DinoGeek offers simple articles on complex technologies

Would you like to be quoted in this article? It's very simple, contact us at dino@eiki.fr

CSS | NodeJS | DNS | DMARC | MAPI | NNTP | htaccess | PHP | HTTPS | Drupal | WEB3 | LLM | Wordpress | TLD | Domain name | IMAP | TCP | NFT | MariaDB | FTP | Zigbee | NMAP | SNMP | SEO | E-Mail | LXC | HTTP | MangoDB | SFTP | RAG | SSH | HTML | ChatGPT API | OSPF | JavaScript | Docker | OpenVZ | ChatGPT | VPS | ZIMBRA | SPF | UDP | Joomla | IPV6 | BGP | Django | Reactjs | DKIM | VMWare | RSYNC | Python | TFTP | Webdav | FAAS | Apache | IPV4 | LDAP | POP3 | SMTP

| Whispers of love (API) | Déclaration d'Amour |






Legal Notice / General Conditions of Use