Render Jinja after jQuery AJAX request to Flask

前端 未结 2 867
梦毁少年i
梦毁少年i 2021-02-01 10:56

I have a web application that gets dynamic data from Flask when a select element from HTML is changed. of course that is done via jquer

2条回答
  •  没有蜡笔的小新
    2021-02-01 11:42

    If you are sending data using json you don't need to use Jinja2. You can simply try something like this:

    @app.route('/_get_content/')
    def _get_content():
        option_id = request.form['option_id']
        all_options = models.Content.query.filter_by(id=option_id)
        return jsonify({'data': [option.name for option in all_options]})
    

    or define a method in your model something like to_json that returns a field or dictionary or ... and call it in your view.

    @app.route('/_get_content/')
    def _get_content():
        option_id = request.form['option_id']
        all_options = models.Content.query.filter_by(id=option_id)
        return jsonify({'data': [option.to_json() for option in all_options]})
    

提交回复
热议问题