【问题标题】:How to put submit buttons for every record in django tables 2?如何为 django 表 2 中的每条记录放置提交按钮?
【发布时间】:2014-08-20 22:02:18
【问题描述】:

我在表单中有一个 django_tables2 表,这是必要的,因为顶部有一个删除选定按钮。在每一行中,我都需要一个带有“接受”、“拒绝”等操作的提交按钮。

但我不知道如何识别按下按钮的行。我怎样才能做到这一点?

【问题讨论】:

    标签: django django-tables2


    【解决方案1】:

    我发现这种方法可以为每一行添加一个按钮,其 id 指向您选择的行:

    在 app/tables.py 中

    import django_tables2 as tables
    from app.models import MyModel
    from django.utils.safestring import mark_safe
    from django.utils.html import escape
    
    class MyColumn(tables.Column): 
        empty_values = list() 
        def render(self, value, record): 
            return mark_safe('<button id="%s" class="btn btn-info">Submit</button>' % escape(record.id))
    
    class MyTable(tables.Table):
        submit = MyColumn()
        class Meta:
            model = MyModel
            ...
    

    在 app/views.py 中,就像往常一样:

    def mypage(request) :
        table = MyTable(MyModel.objects.all())
        RequestConfig(request,paginate=False).configure(table)
        return render(request,'template.html',locals())
    

    然后我使用 Jquery 通过检索每个按钮的 id 来触发我想要的...

    对于您想要做的事情来说,这可能有点太简单了,我已经看到其他解决方案可能使用 LinkColumns 和 Django URL 调度程序更有效:Django-Tables2 LinkColumn link doesn't work

    【讨论】:

      猜你喜欢
      • 2015-06-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-10-20
      • 2012-10-21
      相关资源
      最近更新 更多