Debug School

rakesh kumar
rakesh kumar

Posted on • Updated on

Django Static Files Handling


What are Static files?
Static files are those files which can not be processed, generated or modified by the server.

In the previous section, we learned about the concept of static files. There is a catch here. Images, JavaScript files, etc are types of content or static files. Static files contain all kinds of file types – from .mpeg, .jpeg to .pdf, etc.
There is a simple concept of working with static files. When a user requests for a webpage, the server will generate the HTML. Then the server will collect all the corresponding static files related to that page. Lastly, this whole data is served.

You can see that process in this flow-diagram. Also, we can say that static websites are much faster than dynamic websites.

Image description

Benefits of Static Files
They are static: These files don’t change until the developer replace them with a new one. Thus, the server just fetches them from the disk, taking a minimum amount of time.
Static files are easier to cache: They don’t change and are not modified by the server. That makes the performance faster.
Static files are energy efficient: Static files are fetched from the disk when required. They require no processing which saves the processing overhead and website response becomes fast.
Let’s understand one more important topic, i.e. Template Inheritance.

What is Template Inheritance?
Static files improve the performance of the website. Developers use it more efficiently with template inheritance method and it is one of Django’s key features.

The basic concept is that: We define the common parts of a webpage, such as navigation bars and About section. All these parts are essentially HTML and some static files. Majority of webpages will contain similar sections.

The problem is that when there are lots of webpages, the modification becomes complex. Suppose we want to modify the About section. Then, the developer will have to change the code on every single page. That will lead to chaos and will definitely generate bugs.

Django templating solves this problem, by inheritance. It provides us with the modularity of developing different sections of a webpage. Like we can define a base template, which contains nav bars and About section. Then inherit this page and just add the unique part in other webpages.

Now, let’s implement template inheritance in a project.

Making a Project

  1. Create a New Django application

First, we will create a new Django application. For that, start your virtual environment in command prompt. Then go to the root folder. After that, execute this command in terminal/ PowerShell.


python startapp home
Enter fullscreen mode Exit fullscreen mode

This application will contain all the templates. We will practice on this application in further sections of this article.

Now, install that application in your Django project.

Add this name to INSTALLED_APPS list.

Image description

Define Some View Functions

Let’s define some view functions for this project. Add this code to your home/ file.

from django.shortcuts import render
#DataFlair #Views #TemplateInheritance
# Create your views here.
def home(request):
    return render(request, 'base.html')
def other(request):
    context = {
    'k1': 'Welcome to the Second page',
    return render(request, 'others.html', context)
Enter fullscreen mode Exit fullscreen mode

Image description

These are some simple view functions.

Defining Templates

Now, we can define our templates. First, create a new folder template in the home app. Inside that folder, create two new files, base.html, and others.html.

Then just paste this code in base.html

<!DOCTYPE html>
    <h1>DataFlair Django Tutorials</h1>
{% block body_block %}
<h1>Welcome to the Home-Page</h1>
<h2> search for others/ </h2>
      <h2> we are in the block of home page</h2>
{% endblock %}
Enter fullscreen mode Exit fullscreen mode

Image description

Here, you can note that we are not using the STATIC_ROOT has a different value. But, that’s fine since we defined Root.

You can test it for some errors here. Try executing this command:

python collectstatic

Top comments (0)