title: How to send dictionary back to Django view?
body: <p>In my view I have a dictionary of users, i'm passing it to the template inside args{} dictionary(maybe not the proper way to do that).
  Then when I POST data on the page with AJAX call I want this dictionary to come back to the view(in order to add new entries to it). How can I send it back using JavaScript, jQuery or AJAX?  </p>
  
  <p>If anyone wondering what the hell am I doing:<br>
  I'm doing a constructor for model, so customer can search for users and add them to some kind of a group table(picking one by one), but before I can save the model I need to keep track of users customer added. I feel like using a dictionary for this is not a good way, but even if it would be a list or something I still don't know how to keep data back on AJAX calls.</p>
  
  <p><strong>ADDITION TO ANSWER GIVEN BELOW</strong><br>
  <strong>IF YOU HAVE SIMILAR PROBLEM MAYBE IT IS GOOD TO READ</strong><br>
  I ended up writing everything in one script and note - not in the file, but to the  tag, for some reason the solution and some jquery functions can not work as intended if called from file(weird, I always thought it makes absolutely no difference).<br>
  So yeah, if you follow the answer below you will be able to pass dictionary back to Django view, but the stuff that you will pass is not longer a dictionary it is not unicode string. You need to convert it back to dictionary(if you need to use it like dictionary later).<br>
  You do it with following code:</p>
  
  <pre><code>import ast
  received_dict = request.POST['received_dict']
  newdict = ast.literal_eval(received_dict)
  </code></pre>
  
  <p>But be careful, try to output the dictionary(that was initially sent) on your page for debugging. For some reason sometimes it may contain &#39; instead of "  '   ", I believe it happens if you pass it not to  but to .js file(maybe it was other reason, don't remember). So if you have it you would probably need to do this before ast.literal_eval function:  </p>
  
  <pre><code>received_dict= received_dict.replace("u&amp;#39;", "'") # Maybe you need to delete u also, try for yourself
  received_dict = received_dict.replace("&amp;#39;", "'")
  </code></pre>
  
  <p>But I don't use it, it seems to pass everything OK if Javascript is inside  on the page itself.</p>
  
  <p>Also if anyone wonders I had problems with getting values to script from AJAX generated html, jquery couldn't see the ids and stuff. I solved my problem by changing this(use it as template):  </p>
  
  <pre><code>$('form').on('submit', function(e) {
          // some stuff
      });
  </code></pre>
  
  <p>to this:  </p>
  
  <pre><code>$(document).on('submit', 'form', function(e) {
            // notice changes
              });
  </code></pre>
  
  <p>I think it is called Event Delegation. You put 'document' as selector, then .on and in the brackets first is the action(can be submit, click or whatever) and where it happened(form, div, etc. probably you can use any selector). Sorry if description is stupid I'm new to jquery.<br>
  I hope this helps... multiple ajax requests on one page is pain in the ass :)</p>