在向Flask发出jQuery AJAX请求后渲染Jinja

时间:2022-10-08 17:15:38

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 jquery ajax. No probs here I got that.

我有一个Web应用程序,当HTML中的select元素发生更改时,它会从Flask获取动态数据。当然这是通过jquery ajax完成的。没有probs在这里我得到了。

The problem is, the dynamic data - that is sent by Flask -, is a list of objects from the database Flask-sqlalchemy.

问题是,Flask发送的动态数据是Flask-sqlalchemy数据库中的对象列表。

Of course the data is sent as JSON from Flask.

当然,数据是从Flask发送的JSON。

I'd like to iterate through those objects to display their info using Jinja.

我想迭代这些对象以使用Jinja显示它们的信息。

HTML

<select id="#mySelect">
    <option value="option1" id="1">Option 1 </option>
    <option value="option2" id="1">Option 2 </option> 
    <option value="option3" id="3">Option 3 </option>
</select>

jQuery

$('body').on('change','#mySelect',function(){
   var option_id = $('#mySelect').find(':selected').attr('id');
   $.ajax({
     url: "{{ url_for('_get_content') }}",
     type: "POST",
     dataType: "json",
     data: {'option_id':option_id},
     success: function(data){
       data = data.data;
      /* HERE I WANT TO ITERATE THROUGH THE data LIST OF OBJECTS */
     }

   });
});

Flask

@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': all_options})

PS : I know that jinja gets rendered first so there is no way to assign jQuery variables to Jinja. So how exactly am I going to iterate through the data list if I can't use it in Jinja ?

2 个解决方案

#1


7  

Okay, I got it.

好的,我明白了。

Simply, I made an external html file and added the required jinja template to it.

简单地说,我制作了一个外部html文件并添加了所需的jinja模板。

{% for object in object_list %}
   {{object.name}}
{% endfor %}

then in my Flask file I literally returned the render_template response to the jquery ( which contained the HTML I wanted to append )

然后在我的Flask文件中,我逐字地将render_template响应返回给jquery(其中包含我想要追加的HTML)

objects_from_db = getAllObjects()
return jsonify({'data': render_template('the_temp.html', object_list=objects_from_db)}

And then simply append the HTML from the response to the required div to be updated.

然后只需将响应中的HTML附加到要更新的所需div即可。

#2


0  

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

如果您使用json发送数据,则不需要使用Jinja2。你可以尝试这样的事情:

@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.

或者在模型中定义类似于to_json的方法,该方法返回字段或字典或...并在视图中调用它。

@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]})

#1


7  

Okay, I got it.

好的,我明白了。

Simply, I made an external html file and added the required jinja template to it.

简单地说,我制作了一个外部html文件并添加了所需的jinja模板。

{% for object in object_list %}
   {{object.name}}
{% endfor %}

then in my Flask file I literally returned the render_template response to the jquery ( which contained the HTML I wanted to append )

然后在我的Flask文件中,我逐字地将render_template响应返回给jquery(其中包含我想要追加的HTML)

objects_from_db = getAllObjects()
return jsonify({'data': render_template('the_temp.html', object_list=objects_from_db)}

And then simply append the HTML from the response to the required div to be updated.

然后只需将响应中的HTML附加到要更新的所需div即可。

#2


0  

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

如果您使用json发送数据,则不需要使用Jinja2。你可以尝试这样的事情:

@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.

或者在模型中定义类似于to_json的方法,该方法返回字段或字典或...并在视图中调用它。

@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]})