首页 > 其他 > 详细

Flask -- 静态文件 和 模板渲染

时间:2015-10-30 07:01:54      阅读:190      评论:0      收藏:0      [点我收藏+]

 


静态文件

一般用于存放图片,样式文件(css, js等)

保存位置:包中或者文件所在目录创建一个 static 目录

访问:在应用中使用 /static/...即可访问 

  例如  <link rel="stylesheet" type="text/css" href="/static/css/style.css"> 

       <img src="/static/images/01.jpg"> 

 


模板渲染 

模板引擎:Jinja2

保存位置:应用是个模块,这个文件夹应该与模块同级;

     如果它是一个包,那么这个文件夹作为包的子目录:  

/application.py
/templates
    /hello.html

/application
    /__init__.py
    /templates
        /hello.html

例子:

from flask import render_template

@app.route(/hello/)
@app.route(/hello/<name>)
def hello(name=None):
    return render_template(hello.html, name=name)
<!doctype html>
<title>Hello from Flask</title>
{% if name %}
  <h1>Hello {{ name }}!</h1>
{% else %}
  <h1>Hello World!</h1>
{% endif %}

 

  

Flask -- 静态文件 和 模板渲染

原文:http://www.cnblogs.com/roronoa-sqd/p/4922273.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!