Creating the Best TODO app With the Django Master [pt.3]

Creating the Best TODO app With the Django Master [pt.3]

This is the Third Part of multipart Blog, in this Blog, we will be Looking at the T of MVT architecture of Django.

What is T?

T stands for Templates, a Template in Django is a Html File that is used to Generate a GUI for Average stupid user that cant read the console. Templates are written in Templates directory that can be either in Root directory or the App directory and are rendered by views as a response to specific URLs in

We will be diving into template Inheritance Directly Like a Sigma

  1. Create a Templates directory in the root directory of the project

  2. Create a 'base.html' file in this directory. we will be inheriting this html file

  3. Create another directory named includes inside of the templates directory. Shit like Sidebar, Header, Footer and more site-wide components are kept in that directory. You will Put header.html in this directory

  4. Create a directory named 'home' inside templates directory and create an 'index.html' and 'createtask.html' files. The 'home' directory will help us organize our templates.

  5. If you did everything right, which I highly doubt. This is what your directory structure should look like

SHIT to Read:

Django Uses JINJA templating Engine for template rendering. Read about it here:

How it Works:

In Django the Templates can be inherited, included and more shit like this. Most Rudimentary thing in all of these is Creating Blocks.
we will write base.html and then we will inherit base.html in all other pages and just change the content body and nothing else following DRY concept.


<nav class="navbar navbar-expand-sm navbar-light bg-success">
    <div class="container-fluid">
        <a class="navbar-brand" href="#">To-Do App</a>
        <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarID"
            aria-controls="navbarID" aria-expanded="false" aria-label="Toggle navigation">
            <span class="navbar-toggler-icon"></span>
        <div class="collapse navbar-collapse" id="navbarID">
            <div class="navbar-nav">
                <a class="nav-link " aria-current="page" href="{% url "home" %}">Home</a>
            <div class="navbar-nav">
                <a class="nav-link " aria-current="page" href="{% url "createtask" %}">Add a Task</a>

href="{% url "home" %}" will be converted into href="/"
href="{% url "createtask" %}" will be converted into href="/createtask/"
This all happens dynamically Thanks to Jinja Templating engine


<!doctype html>
<html lang="en">
        <title> Todo|{% block title %}Basefile{% endblock title %}</title>
        <!-- Required meta tags -->
        <meta charset="utf-8" />
            content="width=device-width, initial-scale=1, shrink-to-fit=no"

        <!-- Bootstrap CSS v5.2.1 -->

            {% include "includes/header.html" %}
            {% block content %}{% endblock content %}

        <!-- Bootstrap JavaScript Libraries -->


The {% block title %}Basefile{% endblock title %} is called a Block. If we inherit this
file, we can replace the text "Basefile" with a single line of code and no repeating html code.

The {% include "includes/header.html" %} is called a Include. It is similar to inheriting but we can't change any content of the Parent template easily. We are including header for our site in our base.html in this case.


{% extends "base.html" %}

{% block content %}
            class="table table-striped-columns table-hover table-borderless table-primary align-middle"
            <thead class="table-light">
                    Task List
                    <th>Task ID</th>
                    <th>Task </th>
                    <th>Expires on</th>
            <tbody class="table-group-divider">
                {% for task in tasks %}
                <tr class="table-primary">
                    <td scope="row">{{}}</td>
                    <td>{% if task.expired %}
                        Task has expired.
                        {% else %}
                        {% endif %}</td>
                        {% if task.is_complete %}
                        Task Completed
                        {% else %}
                        Task Pending
                        {% endif %}
                        {% if task.is_complete %}
                        <a class="btn btn-danger" href = {% url 'togglestatus' %} > Mark as incomplete</a>
                        {% else %}
                        <a  class ="btn btn-success" href = {% url 'togglestatus' %} > Mark as Complete</a>
                        {% endif %}
                {% endfor %}



{% endblock content %}

The {% extends "base.html" %} is what's inheriting the base.html template. now we can change just what's inside of the {% block content %}{% endblock content %} and {% block title %}{% endblock title %} that we wrote on base.html file.


{% extends "base.html" %}

{% block content %}
<div class="container">
<form class=" needs-validation form-control" novalidate method="POST">
    {% csrf_token %}
    <button class="btn btn-success" type="submit">Submit form</button>
{% endblock content %}

The {% csrf_token %} is required by default on every post request. CSRF stands for Cross Site Request Forgery. only Nerds get deep into it. We will just use the csrf_token tag.

For Stupid People:

None of the shit inside {{ }} ,{% %} will be rendered on the response, website. It simply is used for Logic part for the engine.
{% tag %} is called a tag.
{{ variable }} is called a variable.

How to RUN the Server:

  1. Open console.

  2. Go to the directory that has file.

  3.    py makemigrations
       py migrate
  4.    py runserver

    GOTO your Microsoft EDGE browser. If you're using any other browser, YOU CANNOT BE A DJANGO MASTER.

This Concludes the Multipart BLOG Creating the Best TODO app With the Django Master [3 pts]

Did you find this article valuable?

Support Nischal lamichhane by becoming a sponsor. Any amount is appreciated!