python-Web-flask——视图内容和模板知识点西宁街

  

<>强基本使用

        #设置cookie值      @app.route ('/set_cookie ')      def set_cookie ():      响应=make_response (“set_cookie”)      response.set_cookie(“名称”、“zhangsan”)      response.set_cookie(“时代”,“13”,10)# 10秒有效期      返回响应      #获取饼干      @app.route ('/get_cookie ')      def get_cookie ():      #获取饼干,可以根据饼干的内容来推荐商品信息      # name=request.cookies['哈哈')      name=request.cookies.get(“名字”)      年龄=request.cookies.get(年龄)      回报”获取饼干,叫% s,年龄是% s“%(姓名、年龄)            #设置SECRET_KEY      app.config [“SECRET_KEY”]=" fhdk ^颗# djefkj& *, *,“      #设置会话      @app.route ('/set_session/& lt;路径:name>”)      def set_session(名称):      会话(“名字”)=名字      会话(“年龄”)=" 13 "      返回“设置会话”      #获取会话内容      @app.route ('/get_session ')      def get_session ():      name=session.get(“名字”)      年龄=session.get(年龄)      回报”的名字是% s,年龄是% s“%(姓名、年龄)      

会话的存储依赖于饼干,在饼干保存的会话编号
  

  

会话编号生成,需要进行加密,所以需要设置secret_key secret_key的作用参考:

  https://segmentfault.com/q/1010000007295395

  

上下文:保存的一些配置信息,比如程序名,数据库连接,应用信息等

  

相当于一个容器,保存了瓶程序运行过程中的一些信息。

  

烧瓶中有两种:请求上下文(会话cookie),应用上下文(current_app g)

  

current_app g是全局变量:

  

current_app.test_https://www.yisu.com/zixun/value='value”

  

g.name=' abc ' # g是一个响应里的全局变量可跨文件

  

<强>渲染模板:

        从进口瓶瓶,render_template      应用=瓶(__name__) #默认省略了三个参数,static_url_path, static_folder template_folders            def添加(a, b):      返回一个+ b      @app.route(“/?      def hello_world ():      #定义数据,整数,字符串,元祖,列表,字典,函数      num=10      str="你好"      元组=(1、2、3、4)      列表=(5、6、7、8)      dict={      “名称”:“张三”,      “年龄”:13      }      返回render_template (file01.html, my_num=num my_str=str, my_tuple=元组,my_list=列表,my_dict=dict类型,添加=增加)      《html》      {{}},{{dict['名字']}},{{dict.get(“名字”)}}和{% %},{{添加(1、2)}}         #模板全局——直接使用      @app.template_global(添加)      def添加(a, b):   返回一个+ b      

过滤器,自定义过滤器

        {{字符串|字符串过滤器}}      安全、低上,小,反向,格式      {#防止转义#}      {{str1 |安全}}或在方法里str2=标记(“& lt; b>只有学习才能让我快乐& lt;/b>”)      {{列表|列表过滤器}}      首先,去年,长度,和,            def do_listreverse(李):      #通过原列表创建一个新列的表      temp_li=列表(李)      #将新列表进行返转      temp_li.reverse ()      返回temp_li      app.add_template_filter (do_listreverse lireverse) #或1      @app.template_filter (lireverse) #或2      def do_listreverse(李):      #通过原列表创建一个新列的表      temp_li=列表(李)      #将新列表进行返转      temp_li.reverse ()      返回temp_li            & lt; h3> my_array原内容:{{my_array}} & lt;/h3>      & lt; h3>my_array反转:{{my_array | lireverse}} & lt;/h3>      

<>强宏,继承,包含

        宏      {%宏观输入(名称、值=" type='文本')%}      & lt; input type="{{类型}}" name="{{名称}}" value=" https://www.yisu.com/zixun/{{value}}”在      {% endmacro %}      {{输入(“名字”,value=' https://www.yisu.com/zixun/zs ')}}//调用      继承      父模板基础:      {%块顶级%}      顶部菜单      {% endblock顶级%}      子模板:      {%延伸的基础。html ' %}      {%块内容%}      需要填充的内容      {% endblock内容%}      包含      {%包括“你好。html ' %}      瓶的模板中特有变量和方法      {{config.DEBUG}}      输出:真      {{request.url}}      输出:http://127.0.0.1      {{g.name}}      {{url_for(家里)}}//url_for会根据传入的路由器函数名,返回该路由对应的URL      {{url_for(“文章”,post_id=1)}}      这个函数会返回之前在烧瓶中通过瓶()传入的消息的列表,flash函数的作用很简单,可以把由Python字符串表示的消息加入一个消息队列中,再使用get_flashed_message()函数取出它们并消费掉      {%的消息get_flashed_messages () %}      {{消息}}      {% endfor %}      模板规则:      & lt;形式行动=" {{url_for(登录)}}”方法=皃ost”比;      & lt;链接rel="样式表" href=" https://www.yisu.com/zixun/{{url_for(“静态”,文件名=css.css)}}“rel=巴獠縩ofollow”在

python-Web-flask——视图内容和模板知识点西宁街