ajax jinja,Render Jinja after jQuery AJAX request to Flask

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.

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

Of course the data is sent as JSON from Flask.

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

HTML

Option 1

Option 2

Option 3

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 ?

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值