imkh
V2EX  ›  Django

Django 如何修改和删除 Web 的表格中的某条记录?

  •  
  •   imkh · Apr 14, 2016 · 4958 views
    This topic created in 3693 days ago, the information mentioned may be changed or developed.

    在 Django 里我用一个页面把数据库的内容列出在一个表格中

    {% for host in hostlist %}
      <tr>
        <td>{{ host.status}}</td>
        <td>{{ host.remark }}</td>
        <td ><button type="button" class="btn btn-default" ><a href="#">删除</a> </button></td>
        <td><button type="button" class="btn btn-default"><a href="#">修改</a></button></td>
      </tr>
    {% endfor %}
    

    如果我想修改表格中的一条记录,该如何实现呢? 想了下,应该是比如取 host.id 的值,再提交到一个修改函数,再把数据从数据库读取出来,显示在表单中,修改后再提交到数据库,但是 host.id 的值怎么样传到修改函数?

    或者有其它方便的方法?

    2 replies    2016-04-15 12:03:13 +08:00
    hehuozhao
        1
    hehuozhao  
       Apr 15, 2016   ❤️ 1
    自己写一条删除的 view ,然后 POST 到个 view 的 URL 。

    `<a href="/delete/{{host.id}}">删除</a>`
    imkh
        2
    imkh  
    OP
       Apr 15, 2016
    @hehuozhao 谢谢
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   3595 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 32ms · UTC 04:57 · PVG 12:57 · LAX 21:57 · JFK 00:57
    ♥ Do have faith in what you're doing.