все. Я начинаю с Python и Django и хочу добавить Ajax в мой код.
Я практикуюсь, показывая продукт на шаблоне вместе с вводом, чтобы изменить его цену.
Я смог изменить его значение в БД, но после этого я хотел бы обновить контекст шаблона, чтобы увидеть новое значение. Поле, которое я хочу получить, является последней строкой таблицы.
{% csrf_token %}
<table>
<tr>
<th>Product</th>
<td>{{product.name}}</td>
</tr>
<tr>
<th>Price</th>
<td><input id="price" type="text" value="{{product.price}}" placeholder="Price" autocomplete="off"/></td>
</tr>
<tr>
<td colspan="2" style='background:none'><input id="send" type="button" value="Update"/></td>
</tr>
<tr>
<th>Current values:</th>
<td>{{product.name}} : ${{product.price}}</td>
</tr>
</table>
JS:
$("#send").click(function(){
$.post("/inicio/acciones/update/", {search: 1, price : $("#price").val()} ).done(function(res){
})
});
Backend
def update(request):
"""backend changes
...
"""
t = loader.get_template('inicio/index.html')
product= Product.objects.get(pk=search)
context = {
'product' : product,
}
t.render(context)
return HttpResponse(product.price)
Var 'product' имеет правильные значения, но мой шаблон не обновляется.
Как мне обновить контекст?