Mastering Django Static Files: Utilizing CSS, Javascript, and Images

Posted by


Django is a popular web framework for building dynamic and interactive websites. In this tutorial, we will learn how to use static files such as CSS, JavaScript, and images in a Django project.

  1. Setting up a Django project:
    Before we can start working with static files in Django, we need to set up a new Django project. To do this, we can use the following command in the terminal:

django-admin startproject project_name
cd project_name
  1. Configuring static files:
    Django provides a built-in way to handle static files. To configure static files in our project, we need to add the ‘django.contrib.staticfiles’ app to our INSTALLED_APPS setting in the settings.py file:
INSTALLED_APPS = [
    ...
    'django.contrib.staticfiles',
]

Next, we need to set up the STATIC_URL and STATIC_ROOT settings in the settings.py file. The STATIC_URL setting defines the base URL for static files, while the STATIC_ROOT setting specifies the directory where Django will collect static files to be served:

STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
  1. Creating a static directory:
    Next, we need to create a ‘static’ directory in our project’s root directory. This directory will be used to store all of our static files such as CSS, JavaScript, and images.
mkdir static
  1. Adding static files:
    To add static files to our project, we need to create subdirectories within the ‘static’ directory for each type of static file. For example, we can create a ‘css’ directory for CSS files, a ‘js’ directory for JavaScript files, and an ‘images’ directory for images.
cd static
mkdir css js images
  1. Linking static files in templates:
    To use static files in our templates, we can use the static template tag provided by Django. This tag generates the URL for a given static file based on the STATIC_URL setting in the settings.py file.

For example, to include a CSS file in a template, we can use the following code:

{% load static %}
<link rel="stylesheet" href="{% static 'css/style.css' %}">

Similarly, to include a JavaScript file in a template, we can use the following code:

{% load static %}
<script src="{% static 'js/script.js' %}"></script>

And to display an image in a template, we can use the following code:

{% load static %}
<img src="{% static 'images/image.jpg' %}" alt="Image">
  1. Handling static files in development and production:
    In the development environment, Django serves static files automatically using the ‘django.contrib.staticfiles’ app. However, in the production environment, we need to collect static files using the ‘collectstatic’ management command provided by Django:
python manage.py collectstatic

This command will collect all of the static files from our apps and libraries and copy them to the STATIC_ROOT directory.

In conclusion, using static files such as CSS, JavaScript, and images in a Django project is essential for creating a visually appealing and interactive website. By following the steps outlined in this tutorial, you can easily work with static files in Django and enhance the functionality and design of your web applications.

0 0 votes
Article Rating

Leave a Reply

4 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
@dibbodas4116
5 hours ago

thank you please complete the series as soon as possible

@armaanalam65
5 hours ago

sir pura complete karana ye series jb jb mujhe time milta hai apki video jrur dekhta hu bhot kuch sikha h apse thank you so much

@sudhirkumar-qe8bt
5 hours ago

Thank you 🙏. Please complete this series ASAP.

@SonuChauhan-lj8gg
5 hours ago

Thank you sir 🙏🙏🙏

4
0
Would love your thoughts, please comment.x
()
x