我有一个显示数据的表,我有一个表单,其中有一个在mysql数据库中插入数据的提交按钮,我在每一行旁边添加了“删除”的按钮,这样我就可以从网站上删除每一行了。在
当我点击按钮时,我得到了id,但我还不知道如何将其传递给视图,但我现在的主要问题是第二篇文章不起作用。在
模板.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
^{2}$视图.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} )
所以,这次我只是想检查post请求是否有效,所以当我单击它时,它将删除具有53 id的行,但它不起作用,所以我想我做错了什么,post无法通过。在
谢谢!在
在一个视图中处理两个(或更多)不同的表单并不是一门科学:您只需要确定发布了哪个表单,这很容易在每个表单中隐藏输入。在
现在,您可以删除无用的jquery内容,并在视图中处理删除操作:
def listpolls(请求): #剪掉与此无关的MySQLdb代码, #请至少使用orm数据库或后端连接
^{pr2}$现在请您自己(以及任何维护您代码的人)做一个服务:学习正确使用Django的ORM,也学习正确使用Python的dbapi。。。这个:
对SQL注入非常开放。正确的方法是
但是在Django中,当你有模型和模型表单时,你真的不需要这个。在
我不能评论但是。所以请将其视为评论。在
我不认为死刑会到达第二个岗位
另外,为什么不使用Django模型而不是显式地使用MySQL呢。在
对于删除一个项目,可以使用jqueryajaxpost-request和该项目的id并在视图中处理它。在
相关问题 更多 >
编程相关推荐