zoukankan      html  css  js  c++  java
  • flask 链接 url_for()

    通常html的文件都放在template里面,那么静态的文件放在哪呢?staitc里面

    调用 url_for('static', filename='css/styles.css', _external=True) 得到的结果是http:// localhost:5000/static/css/styles.css。

    默认设置下,Flask 在程序根目录中名为 static 的子目录中寻找静态文件。如果需要,可在 static 文件夹中使用子文件夹存放文件。
    服务器收到前面那个 URL 后,会生成一个响应, 包含文件系统中 static/css/styles.css 文件的内容。

     请看案例:

    {% block head %}
    {{ super() }}
    <link rel="shortcut icon" href="{{ url_for('static', filename = 'favicon.ico') }}"
    type="image/x-icon">
    <link rel="icon" href="{{ url_for('static', filename = 'favicon.ico') }}"
    type="image/x-icon">
    {% endblock %}

    案例:

    在看代码

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Title</title>
    </head>
    <body>
    <h1>hello, {{ name }}</h1>
    <img src="{{ url_for('static', filename='asd.jpg') }}">
    </body>
    </html>
    静态
    from flask import Flask, render_template
    
    app = Flask(__name__)
    
    
    @app.route('/')
    def index():
        name = "<h1>Hello</h1>"
        return render_template('index.html', name=name)
    
    
    @app.errorhandler(404)
    def url_error(e):
        return render_template('index.html', name=404), 404
    
    
    @app.errorhandler(500)
    def url_error2(e):
        return render_template('500.html'), 500
    
    
    if __name__ == '__main__':
        app.run(debug=True)
    run.py
  • 相关阅读:
    HDU 1850 Being a Good Boy in Spring Festival
    UESTC 1080 空心矩阵
    HDU 2491 Priest John's Busiest Day
    UVALive 6181
    ZOJ 2674 Strange Limit
    UVA 12532 Interval Product
    UESTC 1237 质因子分解
    UESTC 1014 Shot
    xe5 android listbox的 TMetropolisUIListBoxItem
    xe5 android tts(Text To Speech)
  • 原文地址:https://www.cnblogs.com/renfanzi/p/6081179.html
Copyright © 2011-2022 走看看