如何在 django_tables2 中添加复选框列
How to add a checkbox column in django_tables2
我正在尝试添加一个复选框列,以便界面的用户可以 select 行并移动它们。如何将复选框列添加到 table?
我在 django 中找到了 checkboxcolumn.py 文件,但我不知道如何将它正确地实现到我的项目中。
这是我的代码供参考。
tables.py
import django_tables2 as tables
from .models import Model60Mm, Model120Mm, Model122Mm
class Table60mm(tables.Table):
class Meta:
model = Model60Mm
template_name = 'django_tables2/bootstrap.html'
class Table120mm(tables.Table):
class Meta:
model = Model120Mm
template_name = 'django_tables2/bootstrap.html'
class Table122mm(tables.Table):
class Meta:
model = Model122Mm
template_name = 'django_tables2/bootstrap.html'
class DateTable60mm(tables.Table):
shot_time = tables.Column()
class Meta:
model = Model60Mm
order_by = '-shot_name'
template_name = 'django_tables2/bootstrap.html'
views.py
from django.shortcuts import render
from django_tables2 import RequestConfig
from django_tables2 import SingleTableView
from .models import Model60Mm, Model120Mm, Model122Mm
from .tables import Table60mm, Table120mm, Table122mm
def home(request):
return render(request, 'database/home.html')
def sixty(request):
table = Table60mm(Model60Mm.objects.all())
table.paginate(page=request.GET.get('page', 1), per_page=25)
return render(request, 'database/sixty.html', {'table': table})
def onetwenty(request):
table = Table120mm(Model120Mm.objects.all())
table.paginate(page=request.GET.get('page', 1), per_page=25)
return render(request, 'database/onetwenty.html', {'table': table})
def onetwentytwo(request):
table = Table122mm(Model122Mm.objects.all())
table.paginate(page=request.GET.get('page', 1), per_page=25)
return render(request, 'database/onetwentytwo.html', {'table': table})
def sixtydate(request):
table = Table60mm(Model60Mm.objects.all(), order_by='-shot_time')
table.paginate(page=request.GET.get('page', 1), per_page=25)
return render(request, 'database/sixtydate.html', {'table': table})
sixty.html 60mm 模板 table
{% extends 'database/home.html' %}
{% load render_table from django_tables2 %}
{% block content %}
<h1>60MM Data Table</h1>
{% render_table table %}
{% endblock content %}
我建议阅读 https://django-tables2.readthedocs.io/en/latest/pages/custom-data.html
使用提到的 Accessors
和 CheckBoxColumn
自带的 django-tables2,你可以尝试这样的东西:
class DateTable60mm(tables.Table):
your_field = table2.CheckBoxColumn(accessor='your_field ')
然后,your_field
将在您的页面中呈现为复选框。
我发现这个解决方案确实有效:How to get information from Django_tables2 row?
您需要确保 table 包裹在一个表格中。
因为您只需要您的用户 select 行并移动,您的访问器需要是“pk”(主键)
我正在尝试添加一个复选框列,以便界面的用户可以 select 行并移动它们。如何将复选框列添加到 table? 我在 django 中找到了 checkboxcolumn.py 文件,但我不知道如何将它正确地实现到我的项目中。
这是我的代码供参考。
tables.py
import django_tables2 as tables
from .models import Model60Mm, Model120Mm, Model122Mm
class Table60mm(tables.Table):
class Meta:
model = Model60Mm
template_name = 'django_tables2/bootstrap.html'
class Table120mm(tables.Table):
class Meta:
model = Model120Mm
template_name = 'django_tables2/bootstrap.html'
class Table122mm(tables.Table):
class Meta:
model = Model122Mm
template_name = 'django_tables2/bootstrap.html'
class DateTable60mm(tables.Table):
shot_time = tables.Column()
class Meta:
model = Model60Mm
order_by = '-shot_name'
template_name = 'django_tables2/bootstrap.html'
views.py
from django.shortcuts import render
from django_tables2 import RequestConfig
from django_tables2 import SingleTableView
from .models import Model60Mm, Model120Mm, Model122Mm
from .tables import Table60mm, Table120mm, Table122mm
def home(request):
return render(request, 'database/home.html')
def sixty(request):
table = Table60mm(Model60Mm.objects.all())
table.paginate(page=request.GET.get('page', 1), per_page=25)
return render(request, 'database/sixty.html', {'table': table})
def onetwenty(request):
table = Table120mm(Model120Mm.objects.all())
table.paginate(page=request.GET.get('page', 1), per_page=25)
return render(request, 'database/onetwenty.html', {'table': table})
def onetwentytwo(request):
table = Table122mm(Model122Mm.objects.all())
table.paginate(page=request.GET.get('page', 1), per_page=25)
return render(request, 'database/onetwentytwo.html', {'table': table})
def sixtydate(request):
table = Table60mm(Model60Mm.objects.all(), order_by='-shot_time')
table.paginate(page=request.GET.get('page', 1), per_page=25)
return render(request, 'database/sixtydate.html', {'table': table})
sixty.html 60mm 模板 table
{% extends 'database/home.html' %}
{% load render_table from django_tables2 %}
{% block content %}
<h1>60MM Data Table</h1>
{% render_table table %}
{% endblock content %}
我建议阅读 https://django-tables2.readthedocs.io/en/latest/pages/custom-data.html
使用提到的 Accessors
和 CheckBoxColumn
自带的 django-tables2,你可以尝试这样的东西:
class DateTable60mm(tables.Table):
your_field = table2.CheckBoxColumn(accessor='your_field ')
然后,your_field
将在您的页面中呈现为复选框。
我发现这个解决方案确实有效:How to get information from Django_tables2 row?
您需要确保 table 包裹在一个表格中。 因为您只需要您的用户 select 行并移动,您的访问器需要是“pk”(主键)