Skip to main content

How to Add HTML Template in Django

In last tutorial we have seen, how we can return a HTML code through a string from views.py. It is not a good idea to pass whole HTML code through a string because if we have to design a complete webpage in HTML then we shouldn’t pass the whole code through the HttpResponse function because it will reduce the readibility of our code. So instead of passing the whole code through the HttpResponse function, we’ll create a separate HTML file.

In this tutorial we’ll see how we can add a seperate Template (HTML) in django project.

A Template is not only a way for you to separate the HTML from these Python (views.py) but it also gives you the option to plugin some Django code inside those templates so those are not just some simple HTML files, that’s why they have the name Templates. We’ll learn a alot of about Templates in upcoming Django articles.

Create Separate HTML File for Front End (Template)

In our project we need to create a folder that will hold our templates. So go to the top most level of our project where our manage.py file exists, create a folder named as templates. It will be like this:

How to Add HTML Template in Django 1

Now inside that templates folder let’s add our first HTML file. Let’s say we are going to make this file as homepage then just call it as home.html

How to Add HTML Template in Django 2

How to Add HTML Template in Django 3

So now our task is that when someone is requesting the homepage then how we can send them to home.html.

In order to do this follow the steps given below.

Step 1: First we’ve to modify settings.py to tell our server that we’ve added a new template. So open settings.py and go to a variable named as TEMPLATES. Inside we have DIRS. Now you can see square brackets infront of DIRS.

How to Add HTML Template in Django 4

This little brackets is going to be a list of places where it should be looking for the templates. So let’s add the templates directory inside that brackets.

How to Add HTML Template in Django 5

Step 2: As we know when someone is going to request your website, it will check into URLPATTERNS in urls.py file to check is there any path exists for requested URL or not? So we have to make a path for homepage.

How to Add HTML Template in Django 6How to Add HTML Template in Django 6

In above screenshot we’ve a set a path for homepage. If anyone requests our website then it will send that request to views.home function.

Step 3: As above step will send the request to views.home function so we should have a home function inside our views.py. We’ve already created it in our previous tutorial. If you doesn’t have views.py then please create it and add a home function into it like this:

How to Add HTML Template in Django 7

Now we have home function which is returning something.

To open that HTML file, we have to use render function and render function takes two parameters, first one is request object and second is the name of the HTML file to open. To use render function we have to import something that’s why we used:

from django.shortcuts import render

Now we’re all set to go and run our project. To do that we have to run the server first. So open terminal or command prompt and go to the root of your project directory and run this command.

python3 manage.py runserver 

(for who have both python 2 and python 3 installed in their system like Ubuntu 18.04)

or

python manage.py runserver

How to Add HTML Template in Django 8

Now open our website using following link in the above image.

How to Add HTML Template in Django 9

Finally we have our HTML Template opened in browser.

As I mentioned earlier that this HTML file is not any regular HTML file, we can actually run some Python code inside this HTML.

For example: Let’s say I want to send some special information from our views.py to home.html then we can pass a dictionary through the render function as show in image below.

How to Add HTML Template in Django 10

So we’re passing a dictionary here which have a key = name and a value associated with that key is  the crazy programmer.

 Now see how we can show this information in our HTML Template.

Open your home.html templateand edit it as shown in screenshot below:

How to Add HTML Template in Django 11

So here we are going to show our value assigned to that key name. If we pass any key inside two curly braces then it will give us the value of that key. So let’s run it in browser.

How to Add HTML Template in Django 12

Thats how we can pass some information from python function to the HTML template in django.

I am sure your queries like:

  1. How to create a custom HTML template page in django?
  2. How to pass information from Python function views to HTML template in django?

Have been solved.

If you’ve any query related to this tutorial then please let us know in comment box.

The post How to Add HTML Template in Django appeared first on The Crazy Programmer.



from The Crazy Programmer https://www.thecrazyprogrammer.com/2018/11/how-to-add-html-template-in-django.html

Comments

Popular posts from this blog

Rail Fence Cipher Program in C and C++[Encryption & Decryption]

Here you will get rail fence cipher program in C and C++ for encryption and decryption. It is a kind of transposition cipher which is also known as zigzag cipher. Below is an example. Here Key = 3. For encryption we write the message diagonally in zigzag form in a matrix having total rows = key and total columns = message length. Then read the matrix row wise horizontally to get encrypted message. Rail Fence Cipher Program in C #include<stdio.h> #include<string.h> void encryptMsg(char msg[], int key){ int msgLen = strlen(msg), i, j, k = -1, row = 0, col = 0; char railMatrix[key][msgLen]; for(i = 0; i < key; ++i) for(j = 0; j < msgLen; ++j) railMatrix[i][j] = '\n'; for(i = 0; i < msgLen; ++i){ railMatrix[row][col++] = msg[i]; if(row == 0 || row == key-1) k= k * (-1); row = row + k; } printf("\nEncrypted Message: "); for(i = 0; i < key; ++i) f...

Data Encryption Standard (DES) Algorithm

Data Encryption Standard is a symmetric-key algorithm for the encrypting the data. It comes under block cipher algorithm which follows Feistel structure. Here is the block diagram of Data Encryption Standard. Fig1: DES Algorithm Block Diagram [Image Source: Cryptography and Network Security Principles and Practices 4 th Ed by William Stallings] Explanation for above diagram: Each character of plain text converted into binary format. Every time we take 64 bits from that and give as input to DES algorithm, then it processed through 16 rounds and then converted to cipher text. Initial Permutation: 64 bit plain text goes under initial permutation and then given to round 1. Since initial permutation step receiving 64 bits, it contains an 1×64 matrix which contains numbers from 1 to 64 but in shuffled order. After that, we arrange our original 64 bit text in the order mentioned in that matrix. [You can see the matrix in below code] After initial permutation, 64 bit text passed throug...

Accessibility Insights for the Web and Windows makes accessibility even easier

I recently stumbled upon https://accessibilityinsights.io . There's both a Chrome/ Edge extension and a Windows app, both designed to make it easier to find and fix accessibility issues in your websites and apps. The GitHub for the Accessibility Insights extension for the web is at https://github.com/Microsoft/accessibility-insights-web and they have three trains you can get on: Canary (released continuously) Insider (on feature completion) Production (after validation in Insider) It builds on top of the Deque Axe core engine with a really fresh UI. The "FastPass" found these issues with my podcast site in seconds - which kind of makes me feel bad, but at least I know what's wrong! However, the most impressive visualization in my opinion was the Tab Stop test! See below how it draws clear numbered line segments as you Tab from element. This is a brilliant way to understand exactly how someone without a mouse would move through your site. I can easily s...