1

There's a similar titled question on SO, but it really doesn't have anything to do with my issue.

So, I have some JQuery arrays that I create in a function. Now, I would like to set some flask variables to those JS arrays in that same function. The reason I would like to do this is so that I then set the href of an element and route my user.

Here's the current JS code:

JQuery

//Filter Search
$(function() {
    $('#filter_search').click(function() {               
        var entities = JSON.parse(JSON.stringify($('#filterform').serializeObject())).checkboxes;  
        var data = JSON.parse(JSON.stringify($('#filterform').serializeObject())).checkboxeslower;
        if(typeof data == "string") { 
            data = $.makeArray(data); 
        }

        //Here I want to set Flask variables like {{entities}} and {{data}}

        //Then, I will call the next line
        $("#filter_search").attr('href', "{{ url_for('filter_search', entities='{0}', data='{1}'.format(entities, data) )}}");

        //Then, if all goes as planned, it will change the href and redirect the user to filtered_search 
    });
});

1 Answer 1

0

You can't do it the way you want, because the template runs in the server and your jQuery function runs in the client.

The sequence of operations is as follows:

  • the client requests the page
  • the server generates the response by rendering the template. Any url_for() or other Python code in the templates runs at this time
  • the client receives the page and executes the Javascript in it
  • Your jQuery function finally executes, but by then it is too late to change the output of url_for().

What you have to do to make this work is build your URLs in Javascript.

See this answer, where I propose two possible solutions to a similar question.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.