title: Flask flash and url_for with AJAX
body: <p><em>I am currently stuck on a rather "big" problem, and I will try to be as clear and concise as I can about it.
  I am developing a tool in Python, using Flask. It's meant to be an intranet. The idea is that I have a client page. There's its name, numerous other infos, and a massive checklist.</em></p>
  
  <p>The infos are already written in input fields, so that the user just has to edit them there, press enter, and the page is reloaded with the edited information (with a notification, using the flashed messages and growl).</p>
  
  <p>The massive checklist is forcing me to move to a system using AJAX, since I want it to be updated "live" as users tick boxes and enter content in inputs, without reloading the page.</p>
  
  <p>As a consequence, I'm also switching my basic inputs for infos to AJAX, to avoid the reloading of the page.</p>
  
  <p>I've come across two issues :</p>
  
  <ol>
  <li><p>The first one concerns the message flashing feature of Flask.</p>
  
  <p>I've got a method that updates a client name in the database, flashes a message (success or failure, depending on numerous things), then displays the page again. To avoid the page reload, I'm managing the form submit with AJAX.</p>
  
  <p>The problem is, my python method will flash messages. And once I'm back on my html page (that has not been reloaded, since it's AJAX), the <code>get_flashed_message</code> function of Jinja2 returns me nothing, since it has not been updated. As a consequence, it's impossible for me to retrieve those flashed messages.</p>
  
  <p>How can I get these? The only solution I see is getting rid of any usage of flash, and coding my own messages system that I would return from the method, and then treat in javascript. This seems incredibly dumb, since flash is a feature of Flask, and it's made "useless" by AJAX. I feel like I'm missing something.</p>
  
  <p>(As a note, until now, my flashed messages management is made in my base layout, that calls a template that runs through all messages and displays them as growl notifications)</p></li>
  <li><p>Second issue concerns url_for.
  Here is my form, for editing the client's name :</p></li>
  </ol>
  
  <pre class="lang-html prettyprint-override"><code>&lt;form id="changeClientName" method="POST" action="{{ url_for('clients.edit_client_name', name=client.name) }}" style="display:inline !important;"&gt;
      &lt;input type="text" name="name" class="form-control" value="{{ client.name }}" &gt;
      &lt;input type="submit" style="position: absolute; left: -9999px; width: 1px; height: 1px;"/&gt;
  &lt;/form&gt;
  </code></pre>
  
  <p>As you can see, the action attribute uses <code>url_for</code> to call the right method to edit the client. My method looks like this:</p>
  
  <pre class="lang-py prettyprint-override"><code>@mod.route('/edit/&lt;name&gt;/', methods=['POST'])
  def edit_client_name(name):
      # code here    
  </code></pre>
  
  <p>The endspoints before and after an edit to a client name would be <code>/edit/the_client_name</code> and <code>/edit/the_new_client_name</code> as an example. Obviously, once the method has been called, my AJAX has to change this action attribute to replace it with the new URL (in case the user wants to re-edit the name without changing page/reloading the page). And here is my second issue.</p>
  
  <p>The new name is stored in javascript while the action is still the old endpoint. Thus, I have to call <code>url_for</code> using the new name. And I have found no way to pass a Javascript variable to Jinja2.</p>
  
  <p>I want this:</p>
  
  <pre class="lang-py prettyprint-override"><code>url_for('clients.edit_client_name', name=client.name)
  </code></pre>
  
  <p>To become this:</p>
  
  <pre class="lang-py prettyprint-override"><code>url_for('clients.edit_client_name', name=my_javascript_variable_one_way_or_another)
  </code></pre>
  
  <p>I'll just have to call the javascript function that modifies an attribute of a form, and modify my action attribute with this new <code>url_for</code>. But I have found no way to do this, i.e pass a javascript variable to Jinja2.</p>
  
  <hr>
  
  <p>So here are my two issues. How can I make <code>flash()</code> compatible with AJAX? Also, how can I pass a javascript attribute to Jinja2 ?</p>
  
  <p>The only solutions that I can see would be coding my own messaging system, which would defeat the goal of flash, making it useless with AJAX, which seems stupid, and hardcoding the URL in my templates, which totally defeats the initial interest of Flask making URL independent from methods, and URL changes extremely flexible.</p>
  
  <p>Thanks.</p>