How to put submit buttons for every record in django tables 2?

风流意气都作罢 提交于 2019-12-06 17:33:27
GGette1011

I have found this way to add a button to each row with an id that refers to the row you select :

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
        ...

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())

Then I use Jquery to trigger what I want for each button by retrieving their id ...

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!