I have a search form in Django. When the user click in the submit button, the page reload and gets the new elements in a query. I’m trying to do this in an Ajax Query, but when I get the elements in the success option, I don’t know how to change (assign) the new data to the variable.
JS
$.ajax({
url: 'url',
type: "POST",
data: "name=" + selected,
success: function(data){
alert(data);
},
error: function (xhr, textStatus, thrownError){
alert("xhr status: " + xhr.statusText);
},
});
view
def search(request, template_name="search/search.html"):
if request.method == 'POST':
search_terms = request.POST.get('name')
t_places = Place.objects.filter(category__in=search_terms)
places = TranslatedPlace.objects.filter(place__in=t_places)
return HttpResponse(places)
HTML
{% for item in places %}
<li class="search_item">
<div>{{ item.name }}</div>
<div>{{ item.owner }}</div>
<div>{{item.place.category }}</div>
<div>{{item.locality}}, {{item.county}}</div>
</li>
{% endfor %}
When I receive the new data in the ‘data’ variable in Ajax how I can change it in the Django ‘places’ variable?
you can modify the following code inside the ajax success handler
http://jsfiddle.net/3nigma/AueSp/1/