gpt4 book ai didi

jquery - 在同一模板 django 中插入/删除发布请求

转载 作者:行者123 更新时间:2023-11-30 22:34:52 26 4
gpt4 key购买 nike

我有一个显示数据的表格,我有一个带有提交按钮的表单,该按钮将数据插入 mysql 数据库,我在每一行旁边添加了按钮,上面写着“删除”,这样我就可以从网站上删除每一行也是。

我在单击按钮时获得了 id,但我还不知道如何将它传递给 View ,但我现在的主要问题是第二个帖子不起作用。

template.py

<tr>
<td>{{b.ip}}</td>
<td>{{b.polling_time}}</td>
<td>{{b.communitydata}}</td>
<td>{{b.snmp_oid}}</td>
<td>{{b.lastcheck|date:"Y.m.d H:m:s"}}</td>
<form action="/services/listpoll/" method="post">{% csrf_token %}
<td><input type="button" id="{{b.id}}" class="delete_poll" value="Borrar"></td>
</form>
</tr>

jquery

$(".delete_poll").click(function(){

id_poll = $(this).attr('id');

});

views.py

def listpolls(request):
connect_mysql = mdb.connect('***', '***', '***', '***')
cursorMYSQL = connect_mysql.cursor(mdb.cursors.DictCursor)
query = "select id,ip,polling_time,communitydata,snmp_oid,lastcheck from snmptt_listpolls order by ip desc limit 100"
cursorMYSQL.execute(query)
b = cursorMYSQL.fetchall()
connect_mysql.close()

if request.method == 'POST':

form = AddPollForm(request.POST)

if form.is_valid():

ip = form.cleaned_data['poll_ip']
poll_time = form.cleaned_data['poll_time']
communitydata = form.cleaned_data['communitydata']
snmp_oid = form.cleaned_data['snmp_oid']
lastcheck = form.cleaned_data['lastcheck']

cursorMYSQL = connect_mysql.cursor(mdb.cursors.DictCursor)
cursorMYSQL.execute("""insert into snmptt_listpolls (ip, polling_time, communitydata, snmp_oid) values ('%s','%s','%s','%s')"""%(ip, poll_time, communitydata, snmp_oid))

connect_mysql.commit()
connect_mysql.close()

return HttpResponseRedirect('listpolls.html')

elif request.method == 'POST' and not form.is_valid():

id_poll = '53';

cursorMYSQL = connect_mysql.cursor(mdb.cursors.DictCursor)
cursorMYSQL.execute(""" delete from snmptt_listpolls where id='%s' """%(id_poll))

connect_mysql.commit()
connect_mysql.close()

return render_to_response("listpolls.html",{"buffer_data": b, 'form': form} )

else:
form = AddPollForm()
return render_to_response("listpolls.html",{"buffer_data": b, 'form': form} )

所以,这次我只是想检查发布请求是否有效,所以当我单击它时,它会删除 ID 为 53 的行,但它不起作用,所以我想我做错了什么帖子没有通过。

谢谢!

最佳答案

我还不能发表评论。所以请将其视为评论。

我不认为执行会达到第二个帖子

elif request.method=="POST":

另外,为什么不使用 Django 模型而不是使用 MySQL 明确地执行它。

要删除项目,您可以使用带有项目 ID 的 jquery ajax post 请求并在 View 中处理它。

关于jquery - 在同一模板 django 中插入/删除发布请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32868911/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com