Two different ajax urls return the same data
<p>I am using jQuery ajax calls on my django site to populate the options in a select box which gets presented in a modal window that pops up (bootstrap) to the user.  Specifically, it provides a list of available schemas in a particular database.  The ajax url is dynamic which means it includes a db_host and a db_name in the URL.  I have a site with clients and each client has to point to a different host/db.  </p>

<p>The db_host and db_name passed via the URL are used in the django view to execute the necessary sql statement on the relevant host/db.</p>

<p>So, the URL should be unique for each db_host/db_name combination.  </p>

<p>I am running into a problem where I am on client A's page, I click the button to display the modal.  The ajax call is made.  I get the data I expect.  Everything is fine.  Let's say the ajax URL is "/ajax/db_host/server_a/db_name/client_a_db/schema_dropdown".</p>

<p>Now, I got to client B's page and click the same button to display the modal.  The ajax call is made.  Let's say this time, the ajax URL is 
"/ajax/db_host/server_b/db_name/client_b_db/schema_dropdown."  </p>

<p>However, the data returned is the actually the data that was returned for the previous ajax call I made ("/ajax/db_host/server_a/db_name/client_a_db/schema_dropdown") and not for the URL (specifically host/db) that I just passed.</p>

<p>I have double/triple checked that my URL is, in fact different, each time.  Any help would be appreciated.    </p>

<p>Here is my javascript function that gets called just before displaying the modal that populates my select.</p>

<pre><code>function populate_schema_dropdown(db_host, db_name) {

            var ajax_url = "/ajax/db_host/" + db_host + "/db_name/" + db_name + "/schema_dropdown"

            $.ajax({
                url: ajax_url,
                success: function (data) {
                    $("#select-element").empty()
                    $.each(data, function (i, v) {
                        $("#schema-element").append("&lt;option value='" + v[0] + "'&gt;" + v[1] + "&lt;/option&gt;")
                    })

                }
            });
        };
</code></pre>

<p>Below is my django view.</p>

<pre><code>def ajax_schema_dropdown(request, db_host, db_name):

    cursor = get_temporary_db_connection(db_host, db_name).cursor()

    cursor.execute("""
        SELECT 
          NAME
          FROM [sys].[schemas]
          """)
    data = [(each[0], each[0]) for each in cursor.fetchall()]
    cursor.close()
    return HttpResponse(json.dumps(data), content_type="application/json")
</code></pre>

<p>Below is get_temporary_db_connection function...</p>

<pre><code>def get_temporary_db_connection(db_host, db_name):

temporary_name = str(uuid.uuid4)
connections.databases[temporary_name] = {
    'ENGINE': 'sqlserver_ado',
    'NAME': db_name,
    'HOST': db_host,
    'USER': '',
    'PASSWORD': '',
    'OPTIONS': {
        'provider': 'SQLNCLI11'
    }
}

return connections[temporary_name]
</code></pre>

<p>Likely the problem in the way of setting <code>temporary_name</code> variable. It's not dynamic like it suppose to be ( I guess ), that's same database as first one are being used for all proceeding ajax calls, to fix it you have to replace line:</p>

<pre><code>temporary_name  = str(uuid.uuid4)
</code></pre>

<p>to</p>

<pre><code>temporary_name  = str(uuid.uuid4()) # note i've changed uuid.uuid4 -&gt; uuid.uuid4()
________________________________^^____
</code></pre>

<p><strong>Note:</strong> Likely it's make sense close connection to database after working with it, otherwise you will end up with a lot of connections in <code>connections</code> dict. As an option you can keep connection to db in connections dict after it was created and reconnect ( if some condition occured) or simply return existed connection without performing new connection.</p>

javascript jquery python ajax django