如何从帖子上的模板(django)获取class / id名称

时间:2015-09-30 16:13:47

标签: django post request

我需要点击对象的id,以便在视图中的查询中使用它。

模板

<form action="/services/delpoll/" method="post">{% csrf_token %}
       <td><input type="submit" id="{{b.id}}" class="delete_poll" value="Borrar"></td>
     </form>

视图

def delpoll(request):

    connect_mysql = mdb.connect('*', '*', '*', '*')
    cursorMYSQL = connect_mysql.cursor(mdb.cursors.DictCursor)

    if request.method == 'POST':

        id_poll = request.POST.get['delete_poll'];
        cursorMYSQL.execute(""" delete from snmptt_listpolls where id='%s' """%(id_poll))

        connect_mysql.commit()
        connect_mysql.close()

        return render_to_response("delpoll.html",{"message": 'post'} ) 

    else:

        return render_to_response("delpoll.html",{"message": 'no-post'} )  

1 个答案:

答案 0 :(得分:1)

试试这个:

<button type="submit" value="{{b.id}}" name="id_value" class"delete_poll">Borrar</button>

然后在视图中:

request.POST.get("id_value", "")

阅读本文也可以帮到你。 Django Doc