django html模板_如何在Django中添加HTML模板

本文介绍了在Django项目中创建和使用HTML模板的步骤,包括在settings.py中配置模板目录,在urls.py中设置路由,以及在views.py中使用render函数将请求定向到HTML文件。通过这种方式,可以提高代码可读性和实现Python与HTML的交互。
摘要由CSDN通过智能技术生成

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的文件夹。 它将是这样的:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值