Project Layout - 項目結(jié)構(gòu)Application Setup - 應(yīng)用設(shè)置Define and Access the Database - 定義和訪問數(shù)據(jù)庫" />

国产成人精品无码青草_亚洲国产美女精品久久久久∴_欧美人与鲁交大毛片免费_国产果冻豆传媒麻婆精东

15158846557 在線咨詢 在線咨詢
15158846557 在線咨詢
所在位置: 首頁 > 營銷資訊 > 網(wǎng)站運營 > Python 可以做什么?0023-Flask 網(wǎng)站開發(fā)-教程-模板

Python 可以做什么?0023-Flask 網(wǎng)站開發(fā)-教程-模板

時間:2023-05-24 23:33:01 | 來源:網(wǎng)站運營

時間:2023-05-24 23:33:01 來源:網(wǎng)站運營

Python 可以做什么?0023-Flask 網(wǎng)站開發(fā)-教程-模板:

0、背景

從本篇開始翻一下 Flask 教程的文檔,其大綱如下:

本系列文檔:

格瑞圖:Python 可以做什么?0019-Flask 網(wǎng)站開發(fā)-教程-項目結(jié)構(gòu)

格瑞圖:Python 可以做什么?0020-Flask 網(wǎng)站開發(fā)-教程-應(yīng)用設(shè)置

格瑞圖:Python 可以做什么?0021-Flask 網(wǎng)站開發(fā)-教程-定義和訪問數(shù)據(jù)庫

格瑞圖:Python 可以做什么?0022-Flask 網(wǎng)站開發(fā)-教程-藍(lán)圖和視圖

本篇翻譯:模板~

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.
已經(jīng)為應(yīng)用編寫了認(rèn)證視圖,但是運行服務(wù)器后并嘗試訪問任何一個地址,將會看到模板未找到(TemplateNotFound)錯誤。這是應(yīng)為視圖調(diào)用了 render_template(),但是還未編寫任何模板呢。模板文件將會存儲在 flaskr 包的模板(templates)目錄。

本地環(huán)境截圖:

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.
模板就是包含靜態(tài)數(shù)據(jù)和動態(tài)數(shù)據(jù)占位符。模板使用特殊數(shù)據(jù)來渲染成為最終文檔。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.
在應(yīng)用里,將會使用模板來渲染 HTML,HTML 將會在用戶瀏覽器里顯示。在 Flask 里,Jinja 被配置為自動轉(zhuǎn)義 HTML 模板任何數(shù)據(jù)。也就是說可以安全的渲染用戶輸入;任何用戶輸入的字符都有可能搞亂 HTML,錄入左右尖括號,這些都會被安全的轉(zhuǎn)義,不會產(chǎn)生副作用。

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 看起來和派森一樣,表現(xiàn)也一樣。特殊的分隔符用來區(qū)分模板中的 Jinja 語法與靜態(tài)數(shù)據(jù)。在 {{ 和 }} 之間的是表達式,會輸出到最終文檔。{% 和 %} 代表控制流語句,例如 if 和 for 語句。跟派森不一樣的是,代碼塊使用開始和結(jié)束標(biāo)簽,而不是縮進,因為代碼塊中的靜態(tài)文本會改變縮進。

(1)基礎(chǔ)布局 - 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.
應(yīng)用中的每個頁面都有一樣的基礎(chǔ)布局。與其在每個模板中編寫全部的 HTML 結(jié)構(gòu),不如每個模板擴展基礎(chǔ)布局,并重寫一些節(jié)。

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 是否被設(shè)置(從 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.
在頁面標(biāo)題后,在內(nèi)容前,模板循環(huán)每一個從 get_flashed_messages() 返回的消息。在視圖函數(shù)中使用 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.
代碼塊標(biāo)題(title):會改變?yōu)g覽器頁簽和窗體的標(biāo)題。

代碼塊頭(header):跟標(biāo)題類似但是改變的是頁面上的內(nèi)容。

代碼塊內(nèi)容(content):每個頁面要展示的內(nèi)容,例如:登錄窗體或者博客博文。

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.
基礎(chǔ)模板直接放到了模板(templates)目錄。為了便于管理,同一個藍(lán)圖的不同模板將會被組織到藍(lán)圖同名目錄。

(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,這個模板中的內(nèi)容將會替換基礎(chǔ)模板中的塊。所有 {% block %} 里渲染的內(nèi)容都將會覆蓋基礎(chǔ)模板里的。

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 %} 塊里。這樣會在設(shè)置 title 塊時,將其輸出值設(shè)置到 header 塊里,這樣的話窗體標(biāo)題和頁面共享同樣的數(shù)據(jù)而不必編寫雙份代碼。

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.
輸入標(biāo)簽使用了 required 屬性。這就要求瀏覽器在提交表單時,這些字段不能為空。如果用戶使用了舊版本瀏覽器,不支持這個屬性,或者他們使用了非瀏覽器的方式提交了請求,而你仍然需要校驗 Flask 視圖數(shù)據(jù)。在服務(wù)器端完整的校驗數(shù)據(jù)是肥腸重要的,即使客戶端已經(jīng)做了校驗了。

(3)登錄 - Log In

This is identical to the register template except for the title and submit button.
這個跟注冊模板很像,除了標(biāo)題和提交按鈕。

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.
現(xiàn)在已經(jīng)完成了認(rèn)證模板,可以注冊用戶了。確保服務(wù)器仍然在運行(未運行的話執(zhí)行 flask run),然后訪問 http://127.0.0.1:5000/auth/register。

本地環(huán)境截圖:

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() 在表單里顯示了錯誤信息。

本地環(huán)境截圖:

直接點擊注冊按鈕截圖:

移除必須(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.
填寫一個用戶和密碼,將會被重定向到登錄頁面。輸入正確的用戶,或者正確的用戶錯誤的密碼。如果登錄了,將會報一個錯誤:找不到首頁,因為還沒有這個頁面可以重定向。

本地環(huán)境截圖:

僅填寫用戶:

填寫了錯誤密碼:

正確的登錄了,但是沒有主頁:

Continue to Static Files.
請繼續(xù)學(xué)習(xí)靜態(tài)文件。

2、后記

昨兒個卷完忘記發(fā)了~

海兒喂狗(here we go)~

關(guān)鍵詞:模板,教程

74
73
25
news

版權(quán)所有? 億企邦 1997-2025 保留一切法律許可權(quán)利。

為了最佳展示效果,本站不支持IE9及以下版本的瀏覽器,建議您使用谷歌Chrome瀏覽器。 點擊下載Chrome瀏覽器
關(guān)閉