未加星标

How to Add HTML Template in Django

字体大小 | |
[开发(python) 所属分类 开发(python) | 发布者 店小二03 | 时间 2018 | 作者 红领巾 ] 0人收藏点击收藏

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 SeparateHTML 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

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
How to Add HTML Template in Django

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

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

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

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

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

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


How to Add HTML Template in Django

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

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

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

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

I am sure your queries like:

How to create a custom HTML template page in django? 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.

本文开发(python)相关术语:python基础教程 python多线程 web开发工程师 软件开发工程师 软件开发流程

代码区博客精选文章
分页:12
转载请注明
本文标题:How to Add HTML Template in Django
本站链接:https://www.codesec.net/view/610798.html


1.凡CodeSecTeam转载的文章,均出自其它媒体或其他官网介绍,目的在于传递更多的信息,并不代表本站赞同其观点和其真实性负责;
2.转载的文章仅代表原创作者观点,与本站无关。其原创性以及文中陈述文字和内容未经本站证实,本站对该文以及其中全部或者部分内容、文字的真实性、完整性、及时性,不作出任何保证或承若;
3.如本站转载稿涉及版权等问题,请作者及时联系本站,我们会及时处理。
登录后可拥有收藏文章、关注作者等权限...
技术大类 技术大类 | 开发(python) | 评论(0) | 阅读(55)