How to use Django's built-in static file handling for serving static assets

Django is a powerful web framework that makes it easy to create complex web applications. One of the features of Django is its built-in static file handling, which allows you to serve static assets such as images, CSS, and JavaScript files. In this tutorial, we'll show you how to use Django's built-in static file handling for serving static assets.

Install Django

The first step is to install Django. You can do this using the pip command:

pip install django

Once Django is installed, you can create a new project using the django-admin command:

django-admin startproject myproject

Create a Django project

Once you have installed Django, you can create a new project using the django-admin command:

django-admin startproject myproject

This will create a new directory called myproject which contains the files and directories needed for your Django project. You can then change into the myproject directory and start the development server:

cd myproject
python manage.py runserver

This will start the development server on port 8000. You can then open a web browser and navigate to http://localhost:8000 to view your Django project.

Create a static folder

The next step is to create a static folder for your project. This folder will contain all of the static assets such as images, CSS, and JavaScript files. To create the static folder, create a new directory called static in the myproject directory:

mkdir myproject/static

You can then add your static assets to this folder. For example, if you have an image called logo.png, you can add it to the static folder:

cp logo.png myproject/static/

Configure your settings.py file

Once you have created the static folder, you need to configure your settings.py file to tell Django where to find the static files. To do this, open the settings.py file in a text editor and add the following lines:

STATIC_URL = '/static/'
STATICFILES_DIRS = [
os.path.join(BASE_DIR, "static"),
]

This will tell Django to look for static files in the static folder. You can then save the settings.py file and restart the development server.

Add the static tag to your HTML

Once you have configured your settings.py file, you can add the {% static %} tag to your HTML files. This tag will tell Django to look for the static files in the static folder. For example, if you have an image called logo.png in the static folder, you can add it to your HTML file like this:

<img src="{% static 'logo.png' %}" alt="My Logo">

This will tell Django to look for the logo.png file in the static folder and serve it to the browser.

Run the collectstatic command

Once you have added the {% static %} tag to your HTML files, you need to run the collectstatic command. This command will collect all of the static files from the static folder and copy them to the STATIC_ROOT folder. To run the collectstatic command, use the following command:

python manage.py collectstatic

This will collect all of the static files from the static folder and copy them to the STATIC_ROOT folder.

Serve your static assets

Once you have run the collectstatic command, you can serve your static assets using the runserver command. This command will start the development server and serve the static assets from the STATIC_ROOT folder. To start the development server, use the following command:

python manage.py runserver

This will start the development server and serve the static assets from the STATIC_ROOT folder. You can then open a web browser and navigate to http://localhost:8000 to view your Django project.

Conclusion

In this tutorial, we have shown you how to use Django's built-in static file handling for serving static assets. We have shown you how to create a static folder, configure your settings.py file, add the {% static %} tag to your HTML files, run the collectstatic command, and serve your static assets using the runserver command. We hope you have found this tutorial useful.

Useful Links