最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Flask框架學(xué)習(xí)筆記之模板操作實(shí)例詳解

 更新時(shí)間:2019年08月15日 11:14:10   作者:Cytues  
這篇文章主要介紹了Flask框架學(xué)習(xí)筆記之模板操作,結(jié)合實(shí)例形式詳細(xì)分析了flask框架模板引擎Jinja2的模板調(diào)用、模板繼承相關(guān)原理與操作技巧,需要的朋友可以參考下

本文實(shí)例講述了Flask框架學(xué)習(xí)筆記之模板操作。分享給大家供大家參考,具體如下:

flask的模板引擎是Jinja2。

引入模板的好處是增加程序的可讀性和易維護(hù)性,從而不用將一堆html代碼塞在視圖函數(shù)中。

還是以hello world為例。最基礎(chǔ)的調(diào)用模板修飾文本。

# 根網(wǎng)址
@app.route('/')
def index():
  # return render_template("index.html")
  # 可以給模板傳入文本content修飾
  content = "Hello World!"
  return render_template("index.html", content = content)

index模板,用{{}}表示變量。

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Title</title>
</head>
<body>
  <!--<h1>Hello World!</h1>></!-->
  <h1>{{ content }}</h1>
</body>
</html>

這里定義一個(gè)類以傳入變量。

class User(object):
  def __init__(self, user_id, user_name):
    self.user_id = user_id
    self.user_name = user_name

傳參

# 通過調(diào)用類的實(shí)例方法給模板傳遞參數(shù)修飾
@app.route('/user')
def user_index():
  user = User(520, "loli")# user_id, user_name
  return render_template("user_index.html", user=user)

user_index模板,僅顯示user_name。

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Title</title>
</head>
<body>
  <h1>Hello {{ user.user_name }}</h1>
</body>
</html>

在模板中實(shí)現(xiàn)if語句

# 在模板中使用if語句
@app.route('/query_user/<user_id>')
def query_user(user_id):
  user = None
  # 如果傳入的id為520則調(diào)用實(shí)例
  if int(user_id) == 520:
    user = User(520, 'loli')

  return render_template("user_id.html", user=user)

user_id模板,用{% %}包裹if語句,若user不為None(也就是傳入了name),則顯示if下語句,否則顯示else下語句。

最后一定要加上{% endif %}表示判斷語句結(jié)束。

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Title</title>
</head>
<body>
  {% if user %}
    <h1>hello {{ user.user_name }}</h1>
  {% else %}
    <h1>no this user</h1>
  {% endif %}

</body>
</html>


在模板中使用for循環(huán)語句

@app.route('/users')
def user_list():
  users = []
  for i in range(1, 11):
    user = User(i, "loli" + str(i))
    users.append(user)# 將user添加到users
  return render_template("user_list.html", users = users)# 在模板中修飾users

user_list模板,同樣的,for循環(huán)語句也要用{% %}包裹起來,需要用{% endfor %}表示for循環(huán)結(jié)束。這里傳入id和name兩個(gè)參數(shù)。

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Title</title>
</head>
<body>
  {% for user in users %}
    {{ user.user_id }} -- {{ user.user_name }}<br>
  {% endfor %}
</body>
</html>

模板的繼承。模板繼承的初衷也是為了代碼更加簡(jiǎn)單,更易維護(hù),將相同部分的代碼提到一個(gè)基類模板,有點(diǎn)類似于類的繼承。

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Title</title>
</head>
<body>
  <div>
    <h1>I love you {{ user.user_name }}</h1>
  </div>
  {% block content %}
  {% endblock %}
  <div>
    <h1>So much!</h1>
  </div>
</body>
</html>

用<div>圈起來的是不可變的父模板,可改動(dòng)添加的部分在{% block content %}{% endblock %}之間。

子模版,用{% extends "父模板" %} 表示從父模板繼承了代碼。在{% block content %}{% endblock %}之間添加內(nèi)容。

{% extends "base.html" %}
{% block content %}
  <h2>more than anyone</h2>
{% endblock %}
{% extends "base.html" %}
{% block content %}
  <h2>more than anything</h2>
{% endblock %}

調(diào)用

# 模板繼承1
@app.route('/one')
def one_base():
  user = User(520, 'loli')
  return render_template("one_base.html", user=user)
# 模板繼承2
@app.route('/two')
def two_base():
  user = User(520, 'loli')
  return render_template("two_base.html", user=user)


可以看到子模版繼承了題頭和尾部,中間為子模版添加的內(nèi)容。

代碼

#-*- coding:utf-8 -*-
from flask import Flask, render_template# 導(dǎo)入render_template以使用模板
# 定義一個(gè)models導(dǎo)入一個(gè)有id和name的類
from models import User

app = Flask(__name__)
# 根網(wǎng)址
@app.route('/')
def index():
  # return render_template("index.html")
  # 可以給模板傳入文本content修飾
  content = "Hello World!"
  return render_template("index.html", content = content)
# 通過調(diào)用類的實(shí)例方法給模板傳遞參數(shù)修飾
@app.route('/user')
def user_index():
  user = User(520, "loli")
  return render_template("user_index.html", user=user)
# 在模板中使用if語句
@app.route('/query_user/<user_id>')
def query_user(user_id):
  user = None
  if int(user_id) == 520:
    user = User(520, 'loli')

  return render_template("user_id.html", user=user)
# 在模板中使用for循環(huán)語句
@app.route('/users')
def user_list():
  users = []
  for i in range(1, 11):
    user = User(i, "loli" + str(i))
    users.append(user)
  return render_template("user_list.html", users = users)
# 模板繼承1
@app.route('/one')
def one_base():
  user = User(520, 'loli')
  return render_template("one_base.html", user=user)
# 模板繼承2
@app.route('/two')
def two_base():
  user = User(520, 'loli')
  return render_template("two_base.html", user=user)

if __name__ == '__main__':
  app.run()

希望本文所述對(duì)大家基于flask框架的Python程序設(shè)計(jì)有所幫助。

相關(guān)文章

  • Windows和Linux下Python輸出彩色文字的方法教程

    Windows和Linux下Python輸出彩色文字的方法教程

    這篇文章主要介紹了在Windows和Linux中Python輸出彩色文字的方法,通過設(shè)置彩色文字給大家更醒目的效果,文中給出了詳細(xì)的介紹和示例代碼,需要的朋友可以參考借鑒,下面來一起看看吧。
    2017-05-05
  • python創(chuàng)建文件時(shí)去掉非法字符的方法

    python創(chuàng)建文件時(shí)去掉非法字符的方法

    今天小編就為大家分享一篇python創(chuàng)建文件時(shí)去掉非法字符的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2018-10-10
  • 離線狀態(tài)下在jupyter notebook中使用plotly實(shí)例

    離線狀態(tài)下在jupyter notebook中使用plotly實(shí)例

    這篇文章主要介紹了離線狀態(tài)下在jupyter notebook中使用plotly實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2020-04-04
  • python基于phantomjs實(shí)現(xiàn)導(dǎo)入圖片

    python基于phantomjs實(shí)現(xiàn)導(dǎo)入圖片

    這篇文章主要介紹了python基于phantomjs實(shí)現(xiàn)導(dǎo)入圖片的相關(guān)資料,需要的朋友可以參考下
    2016-05-05
  • 如何解決mmcv無法安裝或安裝之后報(bào)錯(cuò)問題

    如何解決mmcv無法安裝或安裝之后報(bào)錯(cuò)問題

    這篇文章主要介紹了如何解決mmcv無法安裝或安裝之后報(bào)錯(cuò)問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2025-04-04
  • Python對(duì)數(shù)據(jù)進(jìn)行插值和下采樣的方法

    Python對(duì)數(shù)據(jù)進(jìn)行插值和下采樣的方法

    今天小編就為大家分享一篇Python對(duì)數(shù)據(jù)進(jìn)行插值和下采樣的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2018-07-07
  • Python中很常用的函數(shù)map()用法實(shí)例

    Python中很常用的函數(shù)map()用法實(shí)例

    這篇文章主要給大家介紹了關(guān)于Python中很常用的函數(shù)map()用法的相關(guān)資料,map()函數(shù)是Python的內(nèi)置函數(shù),會(huì)根據(jù)提供的函數(shù)參數(shù),對(duì)傳入的序列數(shù)據(jù)進(jìn)行映射,需要的朋友可以參考下
    2023-10-10
  • python水晶球(函數(shù))詳解

    python水晶球(函數(shù))詳解

    大家好,本篇文章主要講的是python水晶球(函數(shù))詳解,感興趣的同學(xué)趕快來看一看吧,對(duì)你有幫助的話記得收藏一下,方便下次瀏覽
    2021-12-12
  • Python環(huán)境搭建過程從安裝到Hello World

    Python環(huán)境搭建過程從安裝到Hello World

    這篇文章主要介紹了Python環(huán)境搭建過程從安裝到Hello World,本文通過圖文并茂的形式給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2021-02-02
  • python中字符串String及其常見操作指南(方法、函數(shù))

    python中字符串String及其常見操作指南(方法、函數(shù))

    String方法是用來處理代碼中的字符串的,它幾乎能搞定你所遇到的所有字符串格式,下面這篇文章主要給大家介紹了關(guān)于python中字符串String及其常見操作(方法、函數(shù))的相關(guān)資料,需要的朋友可以參考下
    2022-04-04

最新評(píng)論

喀什市| 玉树县| 西藏| 稻城县| 桂林市| 门源| 达尔| 深州市| 巨鹿县| 宁武县| 武邑县| 满城县| 务川| 红桥区| 广西| 武清区| 峨边| 江都市| 常山县| 姜堰市| 交城县| 天水市| 阿坝| 天全县| 峡江县| 固安县| 柳州市| 泰宁县| 交口县| 若尔盖县| 南宁市| 沂南县| 北川| 荣昌县| 德惠市| 巧家县| 满洲里市| 山丹县| 大庆市| 龙州县| 广平县|