[英]insert / delete post requests in same template django
我有一個顯示數據的表格,並且有一個帶有提交按鈕的表單,該表單將數據插入mysql db中,我在每行旁邊添加了“刪除”按鈕,因此我也可以從站點中刪除每一行。
當我單擊按鈕時我得到了id,但是我還不知道如何將其傳遞給視圖,但是我現在的主要問題是第二篇文章不起作用。
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} )
所以,這一次,我只是想檢查發布請求是否正常,因此當我單擊該請求時,它將刪除具有53 id的行,但是它行不通,所以我想我在做錯誤的事情,並且發布沒有經過。
謝謝!
我目前無法發表評論,因此請視為評論。
我認為執行不會達到第二個職位
elif request.method=="POST":
另外,為什么不使用Django模型而不是通過MySQL明確地使用它。
對於刪除項目,您可以使用帶有該項目ID的jquery ajax post request並在視圖中處理它。
在單個視圖中處理兩個(或多個)不同的表單並不是火箭科學:您只需要確定發布了哪個表單,就可以輕松地通過每個表單中的隱藏輸入來完成。
<td>
<!-- HTML doesn't allow <form> around the <td> -->
<form action="/services/listpoll/" method="post">
{% csrf_token %}
<input type="hidden" name="action" value="delete">
<input type="hidden" name="poll_id" value="{{b.id}}">
<input type="button" class="delete_poll" value="Borrar">
</form>
</td>
現在,您可以擺脫無用的jQuery內容,並在視圖中處理刪除操作:
def listpolls(request):#截斷與此無關的MySQLdb代碼,#請使用orm或至少使用db后端連接
if request.method == 'POST':
if request.post.get("action", "") == "delete":
# don't assume - check
poll_id = request.post.get("poll_id", None)
if poll_id is not None:
delete_poll_here()
else:
form = AddPollForm(request.POST)
# etc
現在,請為您自己(以及任何需要維護您代碼的人)提供一項服務:學習正確使用Django的ORM, 還學習正確使用Python的dbapi ...這:
cursorMYSQL.execute(
"""insert into snmptt_listpolls
(ip, polling_time, communitydata, snmp_oid)
values ('%s','%s','%s','%s')
""" % (ip, poll_time, communitydata, snmp_oid))
對SQL注入開放。 正確的方法是
cursorMYSQL.execute(
"""insert into snmptt_listpolls
(ip, polling_time, communitydata, snmp_oid)
values (%s,%s,%s,%s)
""", (ip, poll_time, communitydata, snmp_oid))
但是當您擁有Models和ModelForms時,在Django中確實不需要此功能。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.