There are a lot of gaps in my knowledge, so I may be trying to solve this the wrong way. The goal is to use this Google Maps JavaScript API to autocomplete and parse address input, and save the results in a model.
I couldn't find a way to pass variables from JS directly to view.py, so now the plan is to have the JS fill a hidden field as suggested by a previous unsolved question.
Could the fields generated by a ModelForm be used for this? If not, how should the form be created so that the JS fills in the fields, the fields are hidden and unchangeable by users, and it makes use of csrf_token?
This is all I have so far. There are a bunch of input fields that get filled automatically by the JS. The form|crispy generated by ModelForm doesn't get filled, even though the id is the same (field names in models.py are the same as Google's naming scheme). I don't understand how the last paragraph of the JS works, that's likely where I'm stuck.
Update: The following code is not giving a POST, why?
<!-- language: lang-js -->
$.ajax({
type: 'POST',
url: 'http://localhost:8000/place/new',
dataType: 'json',
data: {
csrfmiddlewaretoken: '{{ csrf_token }}',
address: 'test'
},
success: function (response, textStatus, error) {
console.log('SUCCESS')
},
error: function (request, textStatus, error) {
console.log('error')
console.log(place)
}
});
<!-- end snippet -->
<!-- language: lang-python -->
def placenew(request):
if request.method == 'POST':
test = request.POST.get('address')
response_data = {}
print (test)
print ("^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^")
return render(request, 'app/placenew.html', {'key':key})
else:
print ("not POST")
return render(request, 'app/placenew.html', {'key':key})
<!-- end snippet -->