Converting any HTML template into a Django template
Tags: django, html, template, djangify
Published: 2020-03-12 18:52:34

# The normal way

Any HTML, CSS, JS or BootStrap template can be converted into a Django compatible template. The trick usually is to add <code>{% raw %}{% static %}{% endraw %}</code> 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](https://dev-to-uploads.s3.amazonaws.com/i/sp84q62llutbd41c7tvp.png)

Template: <a href="https://colorlib.com/wp/template/appco/">Appco</a>

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: 
    <code>django-admin startproject MyProject</code>

### Step 2

cd into the project <code>cd MyProject/</code> and start an app main: 
    <code>python manage.py startapp main</code>

### 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](https://dev-to-uploads.s3.amazonaws.com/i/jhudcioi1d0e5p25g45x.png)

### 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: 
<code>{% raw %}\<"link rel="stylesheet" href="assets/css/bootstrap.min.css">{% endraw %}</code>
needs to be changed to 
<code>{% raw %}\<link rel="stylesheet" href="{% static 'assets/css/bootstrap.min.css' %}">{% endraw %}

### Step 5

We need to include the line <code>{% raw %}{% load static %}{% endraw %}</code> 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 <code>settings.py</code> file by adding <code>'main.apps.MainConfig'</code> in the INSTALLED_APPS list and add a view for creating rendering the index page so modify the MyProject/main/views.py file and add the following: 
<pre>
from django.shortcuts import render

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

and also add a url route for the same, create a file urls.py in MyProject/main/ and add the following code : 
<pre>
from . import views
from django.urls import path

app_name = 'main'

urlpatterns = [
    path('', views.index, name='index'),
]
</pre>

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

<pre>
from django.contrib import admin
from django.urls import path, include

urlpatterns = [
    path('admin/', admin.site.urls),
    path('', include('main.urls', namespace='main')),
]
</pre>

### Step 6

Migrating the changes and running server :
 
<pre> 
python manage.py migrate
python manage.py runserver
</pre> 

We can see that the page loads perfectly: 

![Index Page](https://dev-to-uploads.s3.amazonaws.com/i/p9ku8punx9w9mv9z0gh2.png)

# The djangify way


To ease up the above process, there exists a python package <a href="https://pypi.org/project/djangify/">djangify</a> which can be installed by: 
<code>pip install djangify</code>

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: 

<pre> 
djangify -d main/templates/main
</pre>

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 views.py

Please feel free to drop a message at <a href="https://ohuru.tech/">Ohuru</a> in order to avail various development services offered by us.

{% github https://github.com/L4TTiCe/djangify %}