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

标签 jquery python mysql django

我有一个显示数据的表格,我有一个带有提交按钮的表单,该按钮将数据插入 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/

相关文章:

javascript - 如何优雅地处理 iframe 断路器?

python - 保存在 Models.py 之前对 ManyToManyField 进行验证

python - 获取调用函数的模块和行

php - 根据类别循环结果,但仅显示类别一次

javascript - jQuery 双动画组合

javascript - 引用错误: angular is not defined

Python 内置函数 "compile"。它是干什么用的?

php - 使用PHP向mysql插入多维数组

mysql - 如何从 mySQL 中的字符串中提取前 2 个字符?

javascript - 如何从函数参数谷歌脚本创建文件夹