how to add static files in django 3.2.9

To add static files in Django 3.2.9, you can follow the steps below:

  1. Create a static folder: In your Django project directory, create a folder named "static". This folder will be used to store your static files such as CSS, JavaScript, and images.

  2. Update settings.py: Open your project's settings.py file and locate the STATIC_URL setting. By default, it should be set to '/static/'. Ensure that this setting is present and correctly configured. If not, add or update it as follows:

STATIC_URL = '/static/'
  1. Configure static root: In the same settings.py file, locate the STATIC_ROOT setting. This setting defines the absolute filesystem path to the directory where your static files will be collected during deployment. Add or update it as follows:
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')

Note: The 'staticfiles' directory is just a commonly used name, and you can choose a different name if desired.

  1. Collect static files: Django provides a management command called "collectstatic" to gather all the static files from your various apps and store them in the STATIC_ROOT directory. Open your terminal or command prompt, navigate to your project's root directory, and run the following command:
python manage.py collectstatic

This will copy all the static files from your apps' static folders to the STATIC_ROOT directory.

  1. Load static files in templates: In your HTML templates, you can use the "{% load static %}" template tag to load static files. This tag allows you to reference your static files using the STATIC_URL setting. For example, to include a CSS file named "styles.css", you can use the following code:
{% load static %}
<link rel="stylesheet" type="text/css" href="{% static 'styles.css' %}">

That's it! By following these steps, you should be able to add and use static files in your Django 3.2.9 project. Remember to place your static files in the appropriate directories within your apps' static folders for organization.