如何在Django表2中为每个记录放置提交按钮? [英] How to put submit buttons for every record in django tables 2?
问题描述
我在表单中有一个django_tables2表,这是必要的,因为在顶部有一个删除选定的按钮。在每一行中,我都需要一个带有某些操作的提交按钮,例如接受,拒绝等。
I have a django_tables2 table inside a form, this is necessary because there's a delete selected button on top. In every row, I need a submit button with some action like 'accept', 'deny', etc.
但是我不知道如何识别按下了哪个按钮。我该怎么做呢?
But I don't know how to recognize the row of which the button was pressed. How can I accomplish this?
推荐答案
我发现这种方法可以向每行添加一个按钮,其ID指向您选择的行:
I have found this way to add a button to each row with an id that refers to the row you select :
在app / tables.py
in 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中,只是像往常一样:
in app/views.py, just like usual :
def mypage(request) :
table = MyTable(MyModel.objects.all())
RequestConfig(request,paginate=False).configure(table)
return render(request,'template.html',locals())
然后我使用Jquery触发我想通过获取每个按钮的id来获取每个按钮的内容...
Then I use Jquery to trigger what I want for each button by retrieving their id ...
也许对于您想做的事情来说有点太简单了,也许我看过其他解决方案使用LinkColumns和Django URL调度程序可以提高效率: Django-Tables2 LinkColumn链接无效
Maybe it is a little bit too simple for what you want to do, I've seen other solutions maybe more efficient with LinkColumns and Django URL dispatcher : Django-Tables2 LinkColumn link doesn't work
这篇关于如何在Django表2中为每个记录放置提交按钮?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!