如何在django_tables2中添加复选框列

问题描述 投票:0回答:1

我正在尝试添加复选框列,以便界面的用户可以选择行并将其移动。如何将复选框列添加到表中?我在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表格模板

{% extends 'database/home.html' %}
{% load render_table from django_tables2 %}
{% block content %}
    <h1>60MM Data Table</h1>
    {% render_table table %}
{% endblock content %}
python django checkbox django-tables2
1个回答
0
投票

我建议读https://django-tables2.readthedocs.io/en/latest/pages/custom-data.html

使用提到的AccessorsCheckBoxColumn随附django-tables2,您可以尝试类似的操作:

class DateTable60mm(tables.Table):
    your_field = table2.CheckBoxColumn(accessor='your_field ')

然后your_field将在您的页面中显示为复选框。

© www.soinside.com 2019 - 2024. All rights reserved.