Flask url_for URLs in Javascript

前端 未结 8 2065
粉色の甜心
粉色の甜心 2021-01-30 10:54

What is the recommended way to create dynamic URLs in Javascript files when using flask? In the jinja2 templates and within the python views url_for is used, what i

相关标签:
8条回答
  • 2021-01-30 11:22

    I suggest this:

    <script src="{{ url_for('static', filename='js/jquery.js') }}" type="text/javascript">
    </script>
    

    Works fine for me

    0 讨论(0)
  • 2021-01-30 11:26

    I was trying to call a FLASK route when a button is pressed. It should collect the response and update a div. The page should not reload.

    I used jquery to do this.

    Here's the code:

    @app.route('/<name>')
    def getContent(name):
        return 'This is %s' % name
    

    HTML:

        <div id="mySpace" class="my-3">Count</div>
    
        <button id="next_id" class="btn btn-primary">Press</button>
    
        <script>
            $('#next_id').click(function (){
                $.get('/aroy', function(data, status){
                $('#mySpace').html(data);
            });
            });
        </script>
    
    0 讨论(0)
提交回复
热议问题