django html模板
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.
在上一教程中,我们看到了如何通过views.py中的字符串返回HTML代码。 通过字符串传递整个HTML代码不是一个好主意,因为如果我们必须用HTML设计一个完整的网页,那么我们就不应该通过HttpResponse函数传递整个代码,因为这样会降低代码的可读性。 因此,我们将创建一个单独HTML文件,而不是通过HttpResponse函数传递整个代码。
In this tutorial we’ll see how we can add a seperate Template (HTML) in django project.
在本教程中,我们将看到如何在Django项目中添加单独的模板(HTML)。
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.
模板不仅是您从这些Python(views.py)分离HTML的一种方式,而且还为您提供了在这些模板中插入一些Django代码的选项,因此这些不仅仅是一些简单HTML文件,这就是为什么它们具有名称模板。 在即将发布的Django文章中,我们将学习有关模板的大量知识。
为前端创建单独HTML文件(模板) (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:
在我们的项目中,我们需要创建一个文件夹来保存我们的模板。 因此,转到项目的最顶层(存在manage.py文件),创建一个名为template的文件夹。 它将是这样的: