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

python-Web-flask-視圖內(nèi)容和模板知識(shí)點(diǎn)西寧街

 更新時(shí)間:2019年08月23日 17:03:50   作者:一覺昏睡人  
在本篇文章里小編給大家分享了關(guān)于python-Web-flask-視圖內(nèi)容和模板的相關(guān)知識(shí)點(diǎn)內(nèi)容,有需要的朋友們參考學(xué)習(xí)下。

基本使用

#設(shè)置cookie值

@app.route('/set_cookie')

def set_cookie():

  response = make_response("set_cookie")

  response.set_cookie("name","zhangsan")

  response.set_cookie("age","13",10) #10秒有效期

  return response

#獲取cookie

@app.route('/get_cookie')

def get_cookie():

  #獲取cookie,可以根據(jù)cookie的內(nèi)容來推薦商品信息

  # name = request.cookies['haha']

  name = request.cookies.get('name')

  age = request.cookies.get('age')

return "獲取cookie,name is %s, age is %s"%(name,age)

 

#設(shè)置SECRET_KEY

app.config["SECRET_KEY"] = "fhdk^fk#djefkj&*&*&"

#設(shè)置session

@app.route('/set_session/<path:name>')

def set_session(name):

  session["name"] = name

  session["age"] = "13"

  return "set session"

#獲取session內(nèi)容

@app.route('/get_session')

def get_session():

  name = session.get('name')

  age = session.get('age')

return "name is %s, age is %s"%(name,age)

session的存儲(chǔ)依賴于cookie,在cookie保存的session編號(hào)

session編號(hào)生成,需要進(jìn)行加密,所以需要設(shè)置secret_key secret_key的作用參考:

https://segmentfault.com/q/1010000007295395

上下文:保存的一些配置信息,比如程序名、數(shù)據(jù)庫連接、應(yīng)用信息等

相當(dāng)于一個(gè)容器,保存了 Flask 程序運(yùn)行過程中的一些信息。

Flask中有兩種:請(qǐng)求上下文(session,cookie),應(yīng)用上下文(current_app,g)

current_app,g是全局變量:

current_app.test_value='value'

g.name='abc' # g是一個(gè)響應(yīng)里的全局變量可跨文件

渲染模板:

from flask import Flask,render_template

app = Flask(__name__) #默認(rèn)省略了三個(gè)參數(shù),static_url_path, static_folder, template_folders

 

def adds(a,b):

  return a+b

@app.route('/')

def hello_world():

  #定義數(shù)據(jù),整數(shù),字符串,元祖,列表,字典,函數(shù)

  num = 10

  str = "hello"

  tuple = (1,2,3,4)

  list = [5,6,7,8]

  dict = {

    "name":"張三",

    "age":13

}

return render_template('file01.html',my_num=num,my_str=str,my_tuple=tuple,my_list=list,my_dict=dict,adds=adds)

《html》

{{}},{{dict[‘name']}},{{dict.get(‘name')}}和{%%},{{adds(1,2)}}


# 模板全局--直接使用

@app.template_global('adds')

def adds(a,b):
   return a+b

過濾器&自定義過濾器

{{ 字符串 | 字符串過濾器 }}

Safe,lower,upper,little,reverse,format

{#防止轉(zhuǎn)義#}

{{ str1 | safe}} 或 在方法里str2 = Markup("<b>只有學(xué)習(xí)才能讓我快樂</b>")

{{ 列表 | 列表過濾器 }}

First,last,length,sum,sort
def do_listreverse(li):

  # 通過原列表創(chuàng)建一個(gè)新列表

  temp_li = list(li)

  # 將新列表進(jìn)行返轉(zhuǎn)

  temp_li.reverse()

  return temp_li

app.add_template_filter(do_listreverse,'lireverse') # 或1

@app.template_filter('lireverse') # 或2

def do_listreverse(li):

 # 通過原列表創(chuàng)建一個(gè)新列表

 temp_li = list(li)

 # 將新列表進(jìn)行返轉(zhuǎn)

 temp_li.reverse()

 return temp_li
<h2>my_array 原內(nèi)容:{{ my_array }}</h2>

<h2> my_array 反轉(zhuǎn):{{ my_array | lireverse }}</h2>

宏、繼承、包含

宏

{% macro input(name,value='',type='text') %}

  <input type="{{type}}" name="{{name}}" value="{{value}}">

{% endmacro %}

{{ input('name',value='zs')}} // 調(diào)用

繼承

父模板base:

{% block top %}

 頂部菜單

{% endblock top %}

子模板:

{% extends 'base.html' %}

{% block content %}

 需要填充的內(nèi)容

{% endblock content %}

包含

{% include 'hello.html' %}

Flask 的模板中特有變量和方法

{{config.DEBUG}}

輸出:True

{{request.url}}

輸出:http://127.0.0.1

{{ g.name }}

{{url_for('home')}} // url_for 會(huì)根據(jù)傳入的路由器函數(shù)名,返回該路由對(duì)應(yīng)的URL

{{ url_for('post', post_id=1)}}

這個(gè)函數(shù)會(huì)返回之前在flask中通過flask()傳入的消息的列表,flash函數(shù)的作用很簡(jiǎn)單,可以把由Python字符串表示的消息加入一個(gè)消息隊(duì)列中,再使用get_flashed_message()函數(shù)取出它們并消費(fèi)掉

{%for message in get_flashed_messages()%}

  {{message}}

{%endfor%}

模板規(guī)則:

<form action="{{ url_for('login') }}" method="post">

<link rel="stylesheet" href="{{ url_for('static',filename='css.css') }}" rel="external nofollow" >

web表單

if request.method == 'POST':

    # post請(qǐng)求的數(shù)據(jù)

    print(request.form.get('uname'))

    print(request.form.get('upass'))

    # 存session

    return redirect("/")

# get請(qǐng)求的數(shù)據(jù)

  print(request.args.get('uname'))

  print(request.args.get('upass'))

  # post請(qǐng)求的數(shù)據(jù)

  print(request.form.get('uname'))

  print(request.form.get('upass'))

CSRF

from flask_wtf import CSRFProtect

#設(shè)置SECRET_KEY

app.config["SECRET_KEY"] = "fjkdjfkdfjdk"

#保護(hù)應(yīng)用程序

CSRFProtect(app)
{#設(shè)置隱藏的csrf_token,使用了CSRFProtect保護(hù)app之后,即可使用csrf_token()方法#}

<input type="hidden" name="csrf_token" value="{{ csrf_token() }}">

希望以上整理的內(nèi)容能夠幫助到大家,感謝大家對(duì)腳本之家的支持。

相關(guān)文章

  • python如何實(shí)現(xiàn)圖片轉(zhuǎn)文字

    python如何實(shí)現(xiàn)圖片轉(zhuǎn)文字

    這篇文章主要介紹了python如何實(shí)現(xiàn)圖片轉(zhuǎn)文字問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-03-03
  • Python實(shí)現(xiàn)識(shí)別手寫數(shù)字大綱

    Python實(shí)現(xiàn)識(shí)別手寫數(shù)字大綱

    這篇文章主要為大家詳細(xì)介紹了Python實(shí)現(xiàn)識(shí)別手寫數(shù)字的大綱,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2018-01-01
  • Python處理PDF與CDF實(shí)例

    Python處理PDF與CDF實(shí)例

    今天小編就為大家分享一篇Python處理PDF與CDF實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2020-02-02
  • django+xadmin+djcelery實(shí)現(xiàn)后臺(tái)管理定時(shí)任務(wù)

    django+xadmin+djcelery實(shí)現(xiàn)后臺(tái)管理定時(shí)任務(wù)

    這篇文章主要介紹了django+xadmin+djcelery實(shí)現(xiàn)后臺(tái)管理定時(shí)任務(wù),小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2018-08-08
  • python Protobuf定義消息類型知識(shí)點(diǎn)講解

    python Protobuf定義消息類型知識(shí)點(diǎn)講解

    在本篇文章里小編給大家整理的是一篇關(guān)于python Protobuf定義消息類型知識(shí)點(diǎn)講解,有興趣的朋友們可以學(xué)習(xí)下。
    2021-03-03
  • 利用Django提供的ModelForm增刪改數(shù)據(jù)的方法

    利用Django提供的ModelForm增刪改數(shù)據(jù)的方法

    這篇文章主要介紹了利用Django提供的ModelForm增刪改數(shù)據(jù),小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2019-01-01
  • Python編程中內(nèi)置的NotImplemented類型的用法

    Python編程中內(nèi)置的NotImplemented類型的用法

    這篇文章主要介紹了Python編程中內(nèi)置的NotImplemented類型的用法,NotImplemented 是Python在內(nèi)置命名空間中的六個(gè)常數(shù)之一,下文更多詳細(xì)內(nèi)容需要的小伙伴可以參考一下
    2022-03-03
  • shelve  用來持久化任意的Python對(duì)象實(shí)例代碼

    shelve 用來持久化任意的Python對(duì)象實(shí)例代碼

    這篇文章主要介紹了shelve 用來持久化任意的Python對(duì)象實(shí)例代碼的相關(guān)資料,需要的朋友可以參考下
    2016-10-10
  • 通過實(shí)例解析Python return運(yùn)行原理

    通過實(shí)例解析Python return運(yùn)行原理

    這篇文章主要介紹了通過實(shí)例解析Python return運(yùn)行原理,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-03-03
  • python文件轉(zhuǎn)為exe文件的方法及用法詳解

    python文件轉(zhuǎn)為exe文件的方法及用法詳解

    py2exe是一個(gè)將python腳本轉(zhuǎn)換成windows上的可獨(dú)立執(zhí)行的可執(zhí)行程序(*.exe)的工具,這樣,你就可以不用裝python而在windows系統(tǒng)上運(yùn)行這個(gè)可執(zhí)行程序。本文重點(diǎn)給大家介紹python文件轉(zhuǎn)為exe文件的方法,感興趣的朋友跟隨小編一起看看吧
    2019-07-07

最新評(píng)論

泰宁县| 曲水县| 增城市| 工布江达县| 皋兰县| 江西省| 贵定县| 玉树县| 军事| 宣化县| 界首市| 大邑县| 佳木斯市| 尉犁县| 东乌| 屏东县| 兴安盟| 舒兰市| 新建县| 乡宁县| 容城县| 柯坪县| 蓬溪县| 丹棱县| 潞西市| 镇宁| 白河县| 临潭县| 溆浦县| 合江县| 秭归县| 清苑县| 荣昌县| 杭锦后旗| 西贡区| 株洲市| 镇江市| 林口县| 固始县| 扬州市| 专栏|