当前位置:网站首页>【Flask】静态文件与模板渲染
【Flask】静态文件与模板渲染
2022-07-06 01:26:00 【科皮子菊】
前序文章:
Flask启程与实现一个基于Flask的最小应用程序
静态文件Static Files
静态文件主要是web应用中会使用一些图片,css文件,js文件等去实现一个优雅的网页。这也是动态网站不可或缺的内容。那么Flask会为你解决它。
用Flask文档来说: Just create a folder called static in your package or next to your module and it will be available at /static
on the application. 也就是说,在项目路径中创建static文件夹,或者在模块同一级创建/static
文件夹即可,flask会在这些路径去寻找静态文件内容。
静态文件在系统中也是会有一个路径,假如应用中有个static/style.css
的文件。 那么就可以使用如下方式去定位:
url_for('static', filename='style.css')
模板渲染Rendering Templates
如果使用Python去通过字符串拼接等等方式去构建一个HTML页面,恐怕会把人累死。除此之外,你还不得不考虑注入攻击等。有了这个需求后,就出现了一个模板引擎的概念,通俗的说:这个玩意能够在页面端将html代码以及flask应用返回的数据进行动态整合,然后渲染成html代码在浏览器端展示。如果了解过Java中JSP、Thymeleaf应该就懂了。Flask默认使用到模板渲染引擎是Jinja2.
渲染一个模板(一个html)使用的是render_template()
方法。那么我们所要做的就是提供模板的名称和要作为关键字参数传递给模板引擎的变量。下面是一个如何渲染模板的简单示例:
from flask import render_template
@app.route('/hello/')
@app.route('/hello/<name>')
def hello(name=None):
return render_template('hello.html', name=name)
Flask将会从templates
文件夹中查找对应的文件,这个templates
存放地址与static
类似。可以参考如下理解:
- 模块
/application.py /templates /hello.html
- 包
/application
/__init__.py
/templates
/hello.html
那么对于模板,我们可以使用 Jinja2 模板的全部功能。这也不得不去了解一下Jinja2的文档了:Jinja2.
下面就是一个模板使用案例:
<!doctype html>
<title>Hello from Flask</title>
{
% if name %}
<h1>Hello {
{
name }}!</h1>
{
% else %}
<h1>Hello, World!</h1>
{
% endif %}
在模板中,我们还可以访问 config、request、session 和 g 这些对象以及 url_for()
和 get_flashed_messages()
函数。这些是什么玩意后面再继续介绍,总之可以在模板里直接使用。
一些页面有公共的部分,如页眉、导航和页脚,如果能够把公共的部分放到一个模板里,其他的模板能够简单复用就好了。这就是模板的继承(inheritance)。这个是需要深入了解的,可以参考官方。
启用了自动转义,因此如果name
包含 HTML,它将自动转义。如果我们可以信任一个变量并且知道它是安全的 HTML(例如,因为它来自将 wiki 标记转换为 HTML 的模块),我们可以使用 Markup
类或使用 | safe
过滤器将其标记为安全模板。
Markup
类使用的简单介绍:
from markupsafe import MarkUp
print(Markup('<strong>Hello %s!</strong>') % '<blink>hacker</blink>')
# Markup('<strong>Hello <blink>hacker</blink>!</strong>')
print(Markup.escape('<blink>hacker</blink>'))
# Markup('<blink>hacker</blink>')
print(Markup('<em>Marked up</em> » HTML').striptags())
# Marked up » HTML
总结
好了,今天就了解这么多了。去看看论文咯。
边栏推荐
- How to get all sequences in Oracle database- How can I get all sequences in an Oracle database?
- Leetcode1961. 检查字符串是否为数组前缀
- ORA-00030
- A picture to understand! Why did the school teach you coding but still not
- Mathematical modeling learning from scratch (2): Tools
- Mysql--- query the top 5 students
- How to see the K-line chart of gold price trend?
- 晶振是如何起振的?
- Leetcode 208. 实现 Trie (前缀树)
- CocoaPods could not find compatible versions for pod 'Firebase/CoreOnly'
猜你喜欢
ADS-NPU芯片架构设计的五大挑战
Blue Bridge Cup embedded stm32g431 - the real topic and code of the eighth provincial competition
基於DVWA的文件上傳漏洞測試
Dedecms plug-in free SEO plug-in summary
Finding the nearest common ancestor of binary search tree by recursion
Loop structure of program (for loop)
Recommended areas - ways to explore users' future interests
Poj2315 football games
SSH login is stuck and disconnected
2020.2.13
随机推荐
Five challenges of ads-npu chip architecture design
ThreeDPoseTracker项目解析
About error 2003 (HY000): can't connect to MySQL server on 'localhost' (10061)
Mathematical modeling learning from scratch (2): Tools
Pbootcms plug-in automatically collects fake original free plug-ins
[day 30] given an integer n, find the sum of its factors
DOM introduction
3D model format summary
Leetcode skimming questions_ Sum of squares
ClickOnce 不支持请求执行级别“requireAdministrator”
Leetcode1961. 检查字符串是否为数组前缀
Docker compose配置MySQL并实现远程连接
JMeter BeanShell的基本用法 一下语法只能在beanshell中使用
Gartner released the prediction of eight major network security trends from 2022 to 2023. Zero trust is the starting point and regulations cover a wider range
MCU lightweight system core
关于softmax函数的见解
The basic usage of JMeter BeanShell. The following syntax can only be used in BeanShell
Who knows how to modify the data type accuracy of the columns in the database table of Damon
Leetcode 208. Implement trie (prefix tree)
Vulhub vulnerability recurrence 74_ Wordpress