flask 之url_for()

url_for(endpoint, **values):
用法
1.为动态路由传参
eg: <td><a target="_blank" href="{{ url_for("front.post_detail",post_id=post.id) }}">{{ post.title }}</a></td>
source:
    @bp.route('/p/<post_id>/')
    def post_detail(post_id):
        post = PostModel.query.get(post_id)
        if not post:
            abort(404)
        return render_template('front/front_pdetail.html',post=post)

2.在静态模板中使用
被flask当做特殊路由处理
      url_for('static',filename='css/styles.css',_external=True)
      得到的结果:http://localhost:5000/static/css/styles.css
      <link rel="stylesheet" href="{{ url_for('static',filename='assets/vendor/linearicons/style.css') }}">
      当_external为True时返回url绝对路径,否则返回相对路径

猜你喜欢

转载自blog.csdn.net/weixin_40161254/article/details/83013363
今日推荐