Flask Jinja2更新div内容而不刷新页面

Tyl*_*ler 3 javascript python jquery jinja2 flask

单击联系人,简历,资源链接将需要更新某些功能,例如[ http://webonise.co.uk/] [1 ](位置URL和div内容),但无需刷新页面。

烧瓶查看功能

@app.route('/')
def index():
    flash('Welcome')
    return render_template('index.html')
Run Code Online (Sandbox Code Playgroud)

index.html下base.html的扩展

{% block content %}
    {{super()}}
    <section class="content">
        <a href="#"><i class="mdi-event"></i>Event</a>
        <a href="#"><i class="mdi-contact"></i>Contact</a>
        <div class="board">
            {# dynamic template #}
            {# without using {{% include 'event.html' %}} #}
        </div>
    </section>
{%- endblock %}
Run Code Online (Sandbox Code Playgroud)

{#动态模板#}下单击不同的链接和rendar时,如何刷新rendar event.html / contact.html内容而不刷新页面?

<!--event.html-->
<ul class="list">
    <li>
        <h3>123</h3>
        <p>abc</p>
    </li>
</ul>
Run Code Online (Sandbox Code Playgroud)


我尝试什么

导入jinja2环境,但仍然不知道如何实现这一目标

env = Environment(autoescape=True,
loader=FileSystemLoader(os.path.join(os.path.dirname(__file__), 'templates')))
@app.route('/')
def index():
    board = env.get_template('event.html')
    flash('Welcome Home Tyler')
    return render_template('index.html', board=board)
Run Code Online (Sandbox Code Playgroud)

真的需要ajax技术的get / post方法来实现所有这些功能吗?

sim*_*cci 7

您可以使用Flask-Sijax,它可以帮助您向Flask应用程序添加Sijax支持。Sijax是一个python / jquery库,使AJAX易于在Web应用程序上使用。或者,您可以执行以下操作:

<script>
    $(document).ready( function() {
        $('#next').click(function() {
           $.ajax("{{ url_for('yourroute') }}").done(function (reply) {
              $('#container').html(reply);
           });
        });
    });
</script>

<input type="button" id="next" value="Next" />
<div id="container"></div>
Run Code Online (Sandbox Code Playgroud)

  • 与添加扩展相比,jQuery更容易。 (2认同)