Converting any HTML template into a Django template
djangify django template tutorial

March 2, 2020, 12:38 a.m.

Converting any HTML template into a Django template



A Python script developed by Ohuru that converts HTML Files / Templates to Django compatible HTML Templates. This post will cover the traditional and djangify ways to use pre-built templates with Django.

The normal way

Any HTML, CSS, JS, or BootStrap template can be converted into a Django compatible template. The trick usually is to add {% static %} tags for the js, CSS, and image files that we want to include in the HTML files. That can become a very lengthy task to do for a complete page.

Let us say that we download a template :

Downloading Template

Template: Appco

Let us create a new Django project and try to load the template into it, following are the steps that we would need to take:

Step 1

Create a new django project by: django-admin startproject MyProject

Step 2

cd into the project cd MyProject/ and start an app main: python startapp main

Step 3

Create a new folder called template inside the MyProject and a new folder main inside this templates folder. Now we need to create a folder static/ inside the MyProject/main folder and copy all the static files associated with the project there which in this case reside under the assets folder. So let us directly move the assets folder to the MyProject/main/static/ directory. The file structure will now look something like this: File Structure

Step 4

Now for the most tiresome step, we need to change every link to the CSS / js files in the HTML pages we need to render in the views. Example: <"link rel="stylesheet" href="assets/css/bootstrap.min.css"> needs to be changed to <link rel="stylesheet" href="{% static 'assets/css/bootstrap.min.css' %}"></code>

Step 5

We need to include the line {% load static %} at the top of the HTML page in order to be able to use the static tag.

We need to include the app in the file by adding 'main.apps.MainConfig' in the INSTALLED_APPS list and add a view for creating rendering the index page so modify the MyProject/main/ file and add the following:

from django.shortcuts import render

def index (request): 
    return render(request, 'main/index.html')

and also add a URL route for the same, create a file in MyProject/main/ and add the following code :

from . import views
from django.urls import path

app_name = 'main'

urlpatterns = [
    path('', views.index, name='index'),

now finally we need to add the following in the MyProject/MyProject/ :

from django.contrib import admin
from django.urls import path, include

urlpatterns = [
    path('', include('main.urls', namespace='main')),

Step 6

Migrating the changes and running server :

python migrate
python runserver

We can see that the page loads perfectly:

Index Page

The djangify way

To ease up the above process, there exists a python package djangify which can be installed by:

pip install djangify

The 1-3 and 5-6 steps remain the same but the package automatically does the step 4 for you, after installing the package just run the following command from your shell inside the MyProject directory:

djangify -d main/templates/main

and the tool will create a folder "Modified_files" in which there will be HTML with every CSS, JS reference converted to the Django compatible reference, replacing the index.html in the main/templates/main/ with the same-named file in main/templates/main/Modified_files/ we can easily see the same output as above.

The Modified_files will also contain the other HTML files converted into the desired format and thus can be directly used with

Share this article:


    Jan. 10, 2021, 8:40 p.m.

    Thank you!!1

Leave a comment

Related articles

Flutter signup/login application with Django backend #1

Flutter signup/login application with Django backend #1

Introduction This series of posts intends to develop a flutter signup/login app working with API calls to Django backend. The …

Read Story
Flutter signup/login application with Django backend #2

Flutter signup/login application with Django backend #2

Next steps This post is in continuation of the other post that written on the same topic. If you have …

Read Story
Flutter signup/login application with Django backend #3

Flutter signup/login application with Django backend #3

What this post is about In the previous two posts, we made an API in Django with the help of …

Read Story

Stay right up to date

Get great content to your inbox every week. No spam.
Only great content, we don’t share your email with third parties.