15158846557 在线咨询 在线咨询
15158846557 在线咨询
所在位置: 首页 > 营销资讯 > 网站运营 > Python 可以做什么?0023-Flask 网站开发-教程-模板

Python 可以做什么?0023-Flask 网站开发-教程-模板

时间:2023-05-24 23:33:01 | 来源:网站运营

时间:2023-05-24 23:33:01 来源:网站运营

Python 可以做什么?0023-Flask 网站开发-教程-模板:

0、背景

从本篇开始翻一下 Flask 教程的文档,其大纲如下:

本系列文档:

格瑞图:Python 可以做什么?0019-Flask 网站开发-教程-项目结构

格瑞图:Python 可以做什么?0020-Flask 网站开发-教程-应用设置

格瑞图:Python 可以做什么?0021-Flask 网站开发-教程-定义和访问数据库

格瑞图:Python 可以做什么?0022-Flask 网站开发-教程-蓝图和视图

本篇翻译:模板~

1、模板 -Templates

You've written the authentication views for your application, but if you're running the server and try to go to any of the URLs, you'll see a TemplateNotFound error. That's because the views are calling render_template(), but you haven't written the templates yet. The template files will be stored in the templates directory inside the flaskr package.
已经为应用编写了认证视图,但是运行服务器后并尝试访问任何一个地址,将会看到模板未找到(TemplateNotFound)错误。这是应为视图调用了 render_template(),但是还未编写任何模板呢。模板文件将会存储在 flaskr 包的模板(templates)目录。

本地环境截图:

Templates are files that contain static data as well as placeholders for dynamic data. A template is rendered with specific data to produce a final document. Flask uses the Jinja template library to render templates.
模板就是包含静态数据和动态数据占位符。模板使用特殊数据来渲染成为最终文档。Flask 使用 Jinja 模板库来渲染模板。

In your application, you will use templates to render HTML which will display in the user's browser. In Flask, Jinja is configured to autoescape any data that is rendered in HTML templates. This means that it's safe to render user input; any characters they've entered that could mess with the HTML, such as < and > will be escaped with safe values that look the same in the browser but don't cause unwanted effects.
在应用里,将会使用模板来渲染 HTML,HTML 将会在用户浏览器里显示。在 Flask 里,Jinja 被配置为自动转义 HTML 模板任何数据。也就是说可以安全的渲染用户输入;任何用户输入的字符都有可能搞乱 HTML,录入左右尖括号,这些都会被安全的转义,不会产生副作用。

Jinja looks and behaves mostly like Python. Special delimiters are used to distinguish Jinja syntax from the static data in the template. Anything between {{ and }} is an expression that will be output to the final document. {% and %} denotes a control flow statement like if and for. Unlike Python, blocks are denoted by start and end tags rather than indentation since static text within a block could change indentation.
Jinja 看起来和派森一样,表现也一样。特殊的分隔符用来区分模板中的 Jinja 语法与静态数据。在 {{ 和 }} 之间的是表达式,会输出到最终文档。{% 和 %} 代表控制流语句,例如 if 和 for 语句。跟派森不一样的是,代码块使用开始和结束标签,而不是缩进,因为代码块中的静态文本会改变缩进。

(1)基础布局 - The Base Layout

Each page in the application will have the same basic layout around a different body. Instead of writing the entire HTML structure in each template, each template will extend a base template and override specific sections.
应用中的每个页面都有一样的基础布局。与其在每个模板中编写全部的 HTML 结构,不如每个模板扩展基础布局,并重写一些节。

flaskr/templates/base.html<!doctype html><title>{% block title %}{% endblock %} - Flaskr</title><link rel="stylesheet" href="{{ url_for('static', filename='style.css') }}"><nav> <h1>Flaskr</h1> <ul> {% if g.user %} <li><span>{{ g.user['username'] }}</span> <li><a href="{{ url_for('auth.logout') }}">Log Out</a> {% else %} <li><a href="{{ url_for('auth.register') }}">Register</a> <li><a href="{{ url_for('auth.login') }}">Log In</a> {% endif %} </ul></nav><section class="content"> <header> {% block header %}{% endblock %} </header> {% for message in get_flashed_messages() %} <div class="flash">{{ message }}</div> {% endfor %} {% block content %}{% endblock %}</section>
g is automatically available in templates. Based on if g.user is set (from load_logged_in_user), either the username and a log out link are displayed, or links to register and log in are displayed. url_for() is also automatically available, and is used to generate URLs to views instead of writing them out manually.
g 在模板中自动存在。基于 g.user 是否被设置(从 load_logged_in_user),要么显示用户名和一个注销链接,要么显示一个注册链接和登录。url_for() 同样自动存在,用来生成视图地址而不是手工编写。

After the page title, and before the content, the template loops over each message returned by get_flashed_messages(). You used flash() in the views to show error messages, and this is the code that will display them.
在页面标题后,在内容前,模板循环每一个从 get_flashed_messages() 返回的消息。在视图函数中使用 flash() 来显示错误信息,这里就是显示错误消息的代码。

There are three blocks defined here that will be overridden in the other templates:
这里定义了 3 个代码块,他们会在其他模板里被覆盖:

  1. {% block title %} will change the title displayed in the browser's tab and window title.
  2. {% block header %} is similar to title but will change the title displayed on the page.
  3. {% block content %} is where the content of each page goes, such as the login form or a blog post.
代码块标题(title):会改变浏览器页签和窗体的标题。

代码块头(header):跟标题类似但是改变的是页面上的内容。

代码块内容(content):每个页面要展示的内容,例如:登录窗体或者博客博文。

The base template is directly in the templates directory. To keep the others organized, the templates for a blueprint will be placed in a directory with the same name as the blueprint.
基础模板直接放到了模板(templates)目录。为了便于管理,同一个蓝图的不同模板将会被组织到蓝图同名目录。

(2)注册 - Register

flaskr/templates/auth/register.html{% extends 'base.html' %}{% block header %} <h1>{% block title %}Register{% endblock %}</h1>{% endblock %}{% block content %} <form method="post"> <label for="username">Username</label> <input name="username" id="username" required> <label for="password">Password</label> <input type="password" name="password" id="password" required> <input type="submit" value="Register"> </form>{% endblock %}
{% extends 'base.html' %} tells Jinja that this template should replace the blocks from the base template. All the rendered content must appear inside {% block %} tags that override blocks from the base template.
{% extends 'base.html' %} 告诉 Jinja,这个模板中的内容将会替换基础模板中的块。所有 {% block %} 里渲染的内容都将会覆盖基础模板里的。

A useful pattern used here is to place {% block title %} inside {% block header %}. This will set the title block and then output the value of it into the header block, so that both the window and page share the same title without writing it twice.
这里肥肠有用的模式是,将 {% block title %} 块放置在 {% block header %} 块里。这样会在设置 title 块时,将其输出值设置到 header 块里,这样的话窗体标题和页面共享同样的数据而不必编写双份代码。

The input tags are using the required attribute here. This tells the browser not to submit the form until those fields are filled in. If the user is using an older browser that doesn't support that attribute, or if they are using something besides a browser to make requests, you still want to validate the data in the Flask view. It's important to always fully validate the data on the server, even if the client does some validation as well.
输入标签使用了 required 属性。这就要求浏览器在提交表单时,这些字段不能为空。如果用户使用了旧版本浏览器,不支持这个属性,或者他们使用了非浏览器的方式提交了请求,而你仍然需要校验 Flask 视图数据。在服务器端完整的校验数据是肥肠重要的,即使客户端已经做了校验了。

(3)登录 - Log In

This is identical to the register template except for the title and submit button.
这个跟注册模板很像,除了标题和提交按钮。

flaskr/templates/auth/login.html{% extends 'base.html' %}{% block header %} <h1>{% block title %}Log In{% endblock %}</h1>{% endblock %}{% block content %} <form method="post"> <label for="username">Username</label> <input name="username" id="username" required> <label for="password">Password</label> <input type="password" name="password" id="password" required> <input type="submit" value="Log In"> </form>{% endblock %}

(4)注册用户 - Register A User

Now that the authentication templates are written, you can register a user. Make sure the server is still running (flask run if it's not), then go to http://127.0.0.1:5000/auth/register.
现在已经完成了认证模板,可以注册用户了。确保服务器仍然在运行(未运行的话执行 flask run),然后访问 http://127.0.0.1:5000/auth/register

本地环境截图:

Try clicking the “Register” button without filling out the form and see that the browser shows an error message. Try removing the required attributes from the register.html template and click “Register” again. Instead of the browser showing an error, the page will reload and the error from flash() in the view will be shown.
尝试在未填写表单前,点击一下注册(Register)按钮,看看浏览器是否提示错误信息。尝试从 register.html 模板移除必要(required)属性,并再次点击注册(Register)。这次浏览器显示了错误信息,页面重载并使用 flash() 在表单里显示了错误信息。

本地环境截图:

直接点击注册按钮截图:

移除必须(required)属性截图:

注册 hello@flask

Fill out a username and password and you'll be redirected to the login page. Try entering an incorrect username, or the correct username and incorrect password. If you log in you'll get an error because there's no index view to redirect to yet.
填写一个用户和密码,将会被重定向到登录页面。输入正确的用户,或者正确的用户错误的密码。如果登录了,将会报一个错误:找不到首页,因为还没有这个页面可以重定向。

本地环境截图:

仅填写用户:

填写了错误密码:

正确的登录了,但是没有主页:

Continue to Static Files.
请继续学习静态文件。

2、后记

昨儿个卷完忘记发了~

海儿喂狗(here we go)~

关键词:模板,教程

74
73
25
news

版权所有© 亿企邦 1997-2025 保留一切法律许可权利。

为了最佳展示效果,本站不支持IE9及以下版本的浏览器,建议您使用谷歌Chrome浏览器。 点击下载Chrome浏览器
关闭