django new static files directory

To add a new static files directory in Django, follow these steps:

  1. First, open your Django project in your preferred code editor.

  2. Locate the project's settings.py file. This file is typically located within the main project directory.

  3. Inside the settings.py file, find the STATIC_URL variable. This variable specifies the URL prefix for static files.

  4. Below the STATIC_URL variable, add a new variable called STATICFILES_DIRS. This variable will hold the path(s) to your additional static files directories.

  5. Assign a list of directories to the STATICFILES_DIRS variable. Each directory should be represented as a string. For example, if you want to add a directory called "extra_static" located in your project's root directory, you would write:

STATICFILES_DIRS = [ os.path.join(BASE_DIR, 'extra_static'), ]

Note that you may need to import the os module at the top of your settings.py file to use the os.path.join() function.

  1. Save the settings.py file.

  2. Now, you can place your static files (such as CSS, JavaScript, or images) inside the specified directory. Make sure to maintain the proper directory structure within the new static files directory.

  3. To access these static files in your templates, use the {% load static %} template tag at the top of each template that requires static file inclusion. Then, use the {% static %} template tag to specify the path to the static file. For example:

{% load static %}

This will generate the appropriate URL for the static file based on your STATIC_URL and STATICFILES_DIRS settings.

That's it! You have successfully added a new static files directory in Django. Now, you can organize and serve your static files from the specified directory.