使用JQuery的.load()

时间:2018-11-06 17:12:40

标签: python jquery flask

这是我的wsgi.py

from flask import Flask, render_template, url_for
app = Flask(__name__)

DEVELOPMENT = {}

PRODUCTION = {
    "host": "0.0.0.0",
    "port": "5000"
}

running_mode = DEVELOPMENT

@app.route('/')
def index():
    return render_template("index.html")

@app.route('/#index_content')
def index_content():
    return render_template("index_content.html")

if __name__ == '__main__':
    if running_mode:
        app.run(running_mode["host"], running_mode["port"])
    else:
        app.run()

这是我的index.html

<!doctype html>

<html lang="en">

<head>
    <meta charset="utf-8">

    <title>Gerenciador de Adquirentes</title>
    <meta name="description" content="The HTML5 Herald">
    <meta name="author" content="Concil">
    <link rel="stylesheet" href="{{url_for('static', filename='materialize/css/materialize.min.css')}}">
    <link rel="stylesheet" href="{{url_for('static', filename='concil.css', v=0.01)}}">

</head>

<body>
    <section id="main">

    </section>
    <script src="{{url_for('static', filename='materialize/js/materialize.min.js')}}"></script>
    <script src="{{url_for('static', filename='jquery-3.3.1.min.js')}}"></script>
    <script src="{{url_for('static', filename='hashy-content.js')}}"></script>
    <script>
        $(document).ready(function () {
            $("#main").load("/#index_content");
        });
    </script>
</body>

</html>

当我使用路由'/#index_content'时,它没有发出index_content.html的内容,而是开始发出无休止的请求,从而重新装入了页面。 It doesn't work as in this example。如何动态地在FLASK中加载内容?我不想为每个视图重复html标签。

1 个答案:

答案 0 :(得分:1)

您的/#index_content路线永远不会受到打击,因为#是由浏览器处理的。它永远不会到达烧瓶后端。实际上,flask只会将这个javascript请求视为/(检查request.url以便自己查看),因此实际上发生的是-页面加载后,javascript只会导致页面刷新,因此无限循环。

我建议将端点名称更改为@app.route('/index_content')并点击。

相关问题