静态文件

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

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

访问:在应用中使用 /static/...即可访问 , 更好的方式是使用url_for方法

  例如  <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 %}

 

  

相关文章:

  • 2021-07-14
  • 2021-07-25
  • 2021-08-03
  • 2022-01-08
  • 2021-06-17
  • 2021-11-30
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2021-05-08
  • 2022-12-23
  • 2022-12-23
  • 2021-07-11
  • 2021-09-25
  • 2022-12-23
相关资源
相似解决方案