Bagaimana cara menambahkan kolom kotak centang di Django_tables2

Saya mencoba menambahkan kolom kotak centang sehingga pengguna antarmuka dapat memilih baris dan memindahkannya. Bagaimana cara menambahkan kolom kotak centang ke tabel? Saya telah menemukan file checkboxcolumn.py di Django, tetapi saya tidak tahu bagaimana cara mengimplementasikannya dengan benar ke dalam proyek saya.

Ini kode saya untuk referensi.

tabel.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})

template sixty.html untuk tabel 60mm

{% extends 'database/home.html' %}
{% load render_table from django_tables2 %}
{% block content %}
    <h1>60MM Data Table</h1>
    {% render_table table %}
{% endblock content %}

person Kaley    schedule 23.07.2019    source sumber


Jawaban (2)


Saya menyarankan untuk membaca di https://django-tables2.readthedocs.io/en/latest/pages/custom-data.html

Menggunakan Accessors yang disebutkan dan CheckBoxColumn hadir dengan Django-tables2, Anda dapat mencoba sesuatu seperti:

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

Dan kemudian, your_field akan ditampilkan sebagai kotak centang di halaman Anda.

person fishoverflow    schedule 16.10.2019

Saya menemukan bahwa solusi ini benar-benar berfungsi: Bagaimana cara mendapatkan informasi dari baris Django_tables2?

Anda perlu memastikan bahwa meja dibungkus dalam bentuk. karena Anda hanya perlu pengguna untuk memilih baris dan memindahkan, pengakses Anda harus berupa pk (kunci utama)

person Eniko Kubinyi    schedule 11.02.2021