我试图将地址信息从JS传递到Django views.py
。
目前,我已经在JS中填写了一个隐藏的形式,它是对Django的POST,但是这种方法很容易被用户篡改Inspector中的表单数据(我喜欢数据到正如Google API所说的那样。
这是给出错误的JS:
var objAddress = {};
for (var i = 0; i < place.address_components.length; i++) {
var addressType = place.address_components[i].types[0];
console.log(addressType)
if (componentForm[addressType]) {
var val = place.address_components[i][componentForm[addressType]];
console.log(val)
objAddress[addressType] = val;
}
}
console.log(objAddress);
var address = JSON.stringify(objAddress);
console.log(address);
$.ajax({
type: 'POST',
url: '/place/new/',
data: {
csrfmiddlewaretoken: '{{ csrf_token }}',
address: address
},
success: function (response, textStatus, error) {
console.log('SUCCESS');
},
error: function (request, textStatus, error) {
console.log(error);
console.log(textStatus);
}
});
在Django的一面,它根本没有收到POST。 address
是正确的。我尝试过在data
中发送字符串等变体,但没有任何效果。我对JQuery缺乏经验,而且我现在还不知道还有什么可以看的。
views.py
:
def placenew(request):
if request.method == 'POST':
address = request.POST.get('address')
response_data = {} #what does this do?
print (address)
return render(request, 'app/placenew.html', {'gkey':gkey})
else:
print ("not POST")
return render(request, 'app/placenew.html', {'gkey':gkey})
urls.py
from datetime import datetime
from django.conf.urls import patterns, url, include
from app.forms import BootstrapAuthenticationForm
from rest_framework import routers #REST
from app import views #REST
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns(
'',
url(r'^places/??$', 'app.views.places', name='places'),
url(r'^place/(?P<placeid>[0-9]+)/??$', 'app.views.place', name='place'),
url(r'^place/new/??$', 'app.views.placenew', name='placenew'),
)