通常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

flask 链接 url_for()的更多相关文章

  1. flask之url_for函数

    一:url_for函数 干什么的?传入函数名,得到函数的路由地址(访问视图函数的地址) from flask import Flask from flask import url_for app = ...

  2. python3+Flask 链接MySQL 时,提示“No module named MYSQLdb”

    python3+flask 链接Mysql时提示“No module named MYSQLdb” 解决: pip install mysqlclient

  3. Flask入门 flask结构 url_for 重定向(一)

    Flask入门(一) 1 安装虚拟环境Mac,linux sudo pip install virtualenv ​ ubuntu系统 sudo apt-get install python-virt ...

  4. flask 链接mysql数据库 小坑

    #config.py MYSQL_NAME = 'root' MYSQL_PASSWORD = 'zyms90bdcs' MYSQL_HOST = 'xxxx' MYSQL_POST = ' MYSQ ...

  5. flask中url_for使用endpoint和视图函数名

    在flask中,使用url_for 进行路由反转时,需要传递一个endpoint的值,用法如下: @app.route('/', endpoint='my_index') def index(): r ...

  6. Flask学习 3 url_for的使用

    #!/usr/bin/env python # encoding: utf-8 """ @version: v1.0 @author: cxa @file: flask0 ...

  7. Flask实战第3天:url_for使用

    我们之前是通过url来找到对应的视图函数 /     =>    hello_world 那么url_for则是通过视图函数找到url hello world  =>  / 演示如下 #c ...

  8. flask中的url_for

    转自:https://blog.csdn.net/dengfan666/article/details/78320188 1.URL反转:根据视图函数名称得到当前所指向的url 2.url_for() ...

  9. 3、Flask实战第3天:url_for使用

    我们之前是通过url来找到对应的视图函数 /     =>    hello_world 那么url_for则是通过视图函数找到url hello world  =>  / 演示如下 .. ...

随机推荐

  1. Java字符串方法

    1.字符串和字符数组的转换 .toCharArray() String str1 = "hello"; // //System.out.println(str1); char c[ ...

  2. thikphp创建共享数据config.php

    要求:前台,后台:只需要配置一个config.php 其他文件共享 默认配置是 Index/Conf/config.php Admin/Conf/config.php 代码: return array ...

  3. OC-id、构造方法

    id 简介 万能指针,能指向任何OC对象,相当于NSObject * id类型的定义 typedef struct objc_object { Class isa; } *id; 使用 // 注意:i ...

  4. C- 流程控制(顺序结构,选择结构,循环结构)

    一.选择结构 1.if 特点: 同一时刻,只有一个大括号里面的代码会被执行 2,switch 特点 默认情况下,只有一个case后面的代码会被执行 如果一个case后面没有break,而且这个case ...

  5. C语言打乱一组数字顺序

    #include<stdio.h> #include<stdlib.h> #include<math.h> #include<time.h> int m ...

  6. JSF页面中使用js函数回调后台bean方法并获取返回值的方法

    由于primefaces在国内使用的并不是太多,因此,国内对jsf做系统.详细的介绍的资料很少,即使有一些资料,也仅仅是对国外资料的简单翻译或者是仅仅讲表面现象(皮毛而已),它们的语句甚至还是错误的, ...

  7. svn cleanup failed–previous operation has not finished; run cleanup if it was interrupted

    svn cleanup failed–previous operation has not finished; run cleanup if it was interrupted (2014-08-1 ...

  8. [译] ASP.NET MVC 6 attribute routing – the [controller] and [action] tokens

    原文:http://www.strathweb.com/2015/01/asp-net-mvc-6-attribute-routing-controller-action-tokens/ 当在Web ...

  9. 【bzoj1060】[ZJOI2007]时态同步

    题目描述 小Q在电子工艺实习课上学习焊接电路板.一块电路板由若干个元件组成,我们不妨称之为节点,并将其用数字1,2,3-.进行标号.电路板的各个节点由若干不相交的导线相连接,且对于电路板的任何两个节点 ...

  10. MySQL Cluster 配置文件(config.ini)详解

    MySQL Cluster 配置文件(config.ini)详解 ################################################################### ...