Note that there are some explanatory texts on larger screens.

plurals
  1. PODjango - pass js variable to django view
    primarykey
    data
    text
    <p>My question is related to the Google Maps JavaScript API. More specific the autocomplete function.</p> <p>I've added the autocomplete function as a search bar to my test site and this works fine, but now I want to pass the chosen paramater to a Django view. And here I'm stuck.</p> <p>This is what I have in my template:</p> <pre><code>&lt;script src="https://maps.googleapis.com/maps/api/js?sensor=false&amp;libraries=places"&gt;&lt;/script&gt; &lt;script type="text/javascript"&gt; function initialize() { var input = document.getElementById('searchTextField'); var autocomplete = new google.maps.places.Autocomplete(input); } google.maps.event.addDomListener(window, 'load', initialize); google.maps.event.addListener(autocomplete, 'place_changed', function() { var place = autocomplete.getPlace(); }); &lt;/script&gt; &lt;form method="post" action=""&gt;{% csrf_token %} &lt;input id="searchTextField" type="text" placeholder="Enter a location" autocomplete="on"&gt; &lt;input type="submit" value="Search" class="postfix button"/&gt; &lt;/form&gt; </code></pre> <p>And this in my view:</p> <pre><code>def homepage(request): if request.method == 'POST': location = request.POST else: location = '' return render_to_response('homepage.html', {'location':location}, context_instance = RequestContext(request)) </code></pre> <p>What I want to do is to pass the variable 'place' to the variable 'location' in my Django view (the var place should contain the lat and lon, and this I want to use then further in Geodjango). The <code>request.POST</code> gives the following: <code>&lt;QueryDict: {u'csrfmiddlewaretoken': [u'4xp3nNwzeITb1zHhkBkSGlZSPtGwmuMB']}&gt;</code> But this I can't use.</p> <p>Is it possible to pass the content of var place to the django view?</p> <p>Thanks for the feedback!</p> <p>EDIT: my workaround for the moment using geopy:</p> <p>Template:</p> <pre><code>&lt;input id="searchTextField" name="searchTextField" type="text" placeholder="Enter a location" autocomplete="on"&gt; </code></pre> <p>View:</p> <pre><code>if request.method == 'POST': location = request.POST.get('searchTextField') gn = geocoders.Google() place, (lat, lng) = gn.geocode(location) </code></pre> <p>Still looking for a way to access the <code>var place</code> in the <code>script</code>. Is this possible?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload