jquery - Flask URL routing : url_for only passes first argument -
i have jquery tabbed panel loads pages within tab on click. works fine. want pass additional arguments url such title of tab being clicked. script load following:
$(document).ready(function(){ $( "#content" ).load( "{{url_for(xyz, query=query)}}" ); var tabs = $('.tabs > li'); tabs.on("click", function(){ tabs.removeclass('active'); $(this).addclass('active'); $( "#content" ).load( "{{url_for(xyz, query=query)}}" ); }) });
the html following:
<ul class="tabs clearfix" > <li> <a href=# >all</a> </li> <li class=active > <a href=# >you</a> </li> <li> <a href=# >me</a> </li>
i pass tab title parameter flask url_for. also, best way inspect properties of objects. mean, in python can use dir(object) list object. there simple way of showing attributes of clicked tab object somewhere in browser. thanks
update came with:
var index = $(this).children('a')[0].title;
if want title of tag or
var index = $(this).children('a')[0].innerhtml;
now problem unable pass js variable url_for function. or rather shows none inside function in view
update two have conditional since per understanding cannot pass js variable jinja.
if(index=='a'){ $( "#content" ).load( "{{url_for('xyz', index='a', query=query)}}" ); } else if(index=='b'){ $( "#content" ).load( "{{url_for('xyz', index='b', query=query)}}" ); } else{ $( "#content" ).load( "{{url_for('xyz', query=query)}}" ); }
the problem flask passes first argument while second none. in if blocks values of index goes through while query none while in else block query has value. function inside view
@app.route('/xyz') def xyz(): query = request.args.get("query") index = request.args.get('index')
update 3 have figured out problem is. using string inside jquery load function load content page. url_for creates url of form
/respanes?index=a&query=b
which need when enclosed in quotation marks ampersand converted &
browser not read properly
/respanes?index=a&query=b
so need able escape properly. tried wrap string in encodeuri didnt help.
so had use replace have string want it.
$( "#content" ).load('{{url_for('.xyz, index=a, query=query)}}'.replace('&','&'));
hopefully can come better way of doing this. wanted have tabbed panel load page in through ajax. not sure going through hoopla what's required noob me can glean struggles :)
i'm little late party, encountered same problem. proper solution pretty straightforward: use jinja2 safe
filter force output raw string instead of formatting web-safe entities.
{{url_for('x.y', index=a, query=query) | safe}}
more information on filter available here: http://jinja.pocoo.org/docs/dev/templates/#safe
Comments
Post a Comment