1

I have jQuery tabbed panel that loads pages within the tab on click. This works fine. What I want now is to pass additional arguments to the url such as the title of the tab being clicked. The script to load is the 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 is the 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 would like to pass the tab title as another parameter to my flask url_for. Also, What is the best way to inspect the properties of objects. I mean, in python I can use dir(object) to list everything about that object. Is there a simple way of showing the attributes of a clicked tab object somewhere in the browser. Thanks

UPDATE This is what I came up with:

var index = $(this).children('a')[0].title;

If you want the title of the a tag Or

var index = $(this).children('a')[0].innerHTML;

Now the problem is that I am unable to pass this js variable to the url_for function. Or rather it shows up as None inside the function in my view

UPDATE TWO I now have a conditional since as per my understanding I cannot pass js variable to 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 now is that flask only passes the first argument while the second is None. So in the if blocks the values of index goes through while the query is None while in the else block the query has a value. The function inside view is

@app.route('/xyz')
def xyz():
    query = request.args.get("query")
    index = request.args.get('index')

UPDATE 3 I have figured out what the problem is. I am using the string inside jQuery load function to load up the content from the page. url_for creates a url of the form

/respanes?index=a&query=b

which is what I need but then when its enclosed in the quotation marks the ampersand is converted into &amp; which the browser does not read properly

/respanes?index=a&amp;query=b

So now I need to just be able to escape it properly. I tried to wrap the string in encodeURI but that didnt help.

So I finally had to use replace to exactly have the string as I want it.

 $( "#content" ).load('{{url_for('.xyz, index=a, query=query)}}'.replace('&amp;','&'));

Hopefully someone can come up with a better way of doing this. All I wanted was to have tabbed panel that will load a page in it through ajax. i am not sure going through all this hoopla is what's required but hopefully some noob like me can glean from my struggles :)

Fizi
  • 1,749
  • 4
  • 29
  • 55
  • Is Jinja temptlating changing the actual page? – John Jun 10 '15 at 16:26
  • my question is why would you load pages that way? why not control it via requests where you can send data in the body of the request? – John Ruddell Jun 10 '15 at 16:31
  • I am kind of new to the flask, jinja world so this is just how I made things to work. I am open to a better way of doing it but I believe in this scenario all I need is a jquery functionality that gets me the title of the tab being clicked. Non? – Fizi Jun 10 '15 at 16:36
  • Hmm.. I don't immediately see any issue with what your doing. Can you print the output of the `url_for` calls in the template and also print the contents of `request.args` in the view file? – junnytony Jun 10 '15 at 21:43
  • please look at my last update. Any help will be deeply appreciated. I have spent way too much time on it :( – Fizi Jun 10 '15 at 21:50

1 Answers1

3

I'm a little late to the party, but I encountered this same problem. The proper solution is pretty straightforward: use the Jinja2 safe filter to force it output the raw string instead of formatting it with web-safe entities.

{{url_for('x.y', index=a, query=query) | safe}}

More information on the filter is available here: http://jinja.pocoo.org/docs/dev/templates/#safe

Austin Cory Bart
  • 2,159
  • 2
  • 19
  • 32