我试图想办法让最终用户通过AJAX删除待售物品(如果他们是卖家)。我确定下面的代码有很多错误,但我的问题特别是下面的jQuery AJAX函数。
是' id'以'输入名称'并发送给我的意见?我这样做了吗?
TEMPLATE.HTML
{% for entry in latest_entries %}
entry.name <div class="delete_button" id="{{ entry.name }}">delete</div>
entry.price
entry.picture
entry.sellername
{% endfor %}
JQUERY
<script>
$(document).ready(function() {
$(".delete_button").click(function() {
var id = $(this).attr('id');
$.ajax({
type: "POST",
url: "/storefront/",
data: { entryname:id },
success: function(response){
alert(response.success);
}
});
return false;
});
});
</script>
VIEWS.PY
latest_entries=Entry.objects.order_by('-pub_date')[:16]
@login_required
def delete_object(request):
if request.is_ajax():
object_name = request.POST.get('entryname')
Entry.objects.get(id=object_name).delete()
return HttpResponseRedirect('/storefront/')
答案 0 :(得分:1)
您正在将ID保存到变量id
var id = $(this).attr('id');
但您要发送变量entryname
(不存在,undefined
)
data: { entryname:entryname },
你想要它:
data: { entryname: id },
<强> 更新 强>
您还缺少"
entry.name <div class="delete_button" id="{{ entry.name }}">delete</div>
答案 1 :(得分:0)
你有
data: { entryname:entryname },
但未定义entryname
个变量。定义了id
变量。也许它应该是
data: { entryname:id },