在 Django 框架中将数据从数据库拉取到表中
Pull data from database to tables in Django framework
如何从数据库中提取数据到 HTML tables 在 django 中?
我已经建立了模型。
class device:
track_no=models.CharField(max_length=100)
dev_type=models.CharField(max_length=50)
dev_name=models.CharField(max_length = 100)
我想制作一个表格,我可以在其中用 "device" table 的数据填充 table。另外,我希望 table 中有一个额外的字段作为每行前面的复选框。
请提及所有需要的文件。
我向您推荐一个非常好的 Django 应用程序,名为“Django Tables2”
安装完成后,需要在已安装的应用中添加django_tables2
它们非常易于使用,您需要在与 settings.py models.py 相同的文件夹中创建一个文件 tables.py。 ..
在此 tables.py 文件中,您必须使用您的模型之一添加新的 table,例如:
import django_tables2 as tables
from models import *
class DeviceTable(tables.Table):
# row_id used to have each ID in a first hidden row
row_id = tables.columns.TemplateColumn(attrs={'cell': {'style':'display:none'}}, template_code=u'<span id="row_id_{{ record.id }}">', orderable=False, verbose_name=u'Row ID')
name = tables.columns.TemplateColumn(template_code=u'{{ record.dev_name }}', orderable=True, verbose_name=u'Device Name'))
checkbox = tables.columns.TemplateColumn(template_code=u'<input type="checkbox" >',orderable=False, verbose_name=u'Checkbox')
class Meta:
model = Device
attrs = {'class': 'myClass'} #Add table classes here
fields = ('row_id', 'name', 'track_no', 'dev_type','checkbox')
sequence = fields
order_by = ('name', )
您可以自定义字段或添加新字段,文档解释得很好。创建 table 后,您需要在视图中加载 table:
from django_tables2 import RequestConfig
from tables import DeviceTable
def yourView(request, ...):
# ... Your actual code ...
# We get the object list
device_list = Device.objects.all()
# We pass the object list to the table
table = DeviceTable(device_list)
# RequestConfig is used to automatically add pagination to the table
RequestConfig(request, paginate={"per_page": 10}).configure(table)
return render_to_response('your_template.html', {'table': table, }, context_instance=RequestContext(request))
要在您的模板中呈现此 table,您需要加载 template_tag 以呈现 table:
{% load render_table from django_tables2 %}
# ... Your other code ...
<div class="col-md-12">
{% render_table table %}
</div>
如何从数据库中提取数据到 HTML tables 在 django 中?
我已经建立了模型。
class device:
track_no=models.CharField(max_length=100)
dev_type=models.CharField(max_length=50)
dev_name=models.CharField(max_length = 100)
我想制作一个表格,我可以在其中用 "device" table 的数据填充 table。另外,我希望 table 中有一个额外的字段作为每行前面的复选框。 请提及所有需要的文件。
我向您推荐一个非常好的 Django 应用程序,名为“Django Tables2”
安装完成后,需要在已安装的应用中添加django_tables2
它们非常易于使用,您需要在与 settings.py models.py 相同的文件夹中创建一个文件 tables.py。 ..
在此 tables.py 文件中,您必须使用您的模型之一添加新的 table,例如:
import django_tables2 as tables
from models import *
class DeviceTable(tables.Table):
# row_id used to have each ID in a first hidden row
row_id = tables.columns.TemplateColumn(attrs={'cell': {'style':'display:none'}}, template_code=u'<span id="row_id_{{ record.id }}">', orderable=False, verbose_name=u'Row ID')
name = tables.columns.TemplateColumn(template_code=u'{{ record.dev_name }}', orderable=True, verbose_name=u'Device Name'))
checkbox = tables.columns.TemplateColumn(template_code=u'<input type="checkbox" >',orderable=False, verbose_name=u'Checkbox')
class Meta:
model = Device
attrs = {'class': 'myClass'} #Add table classes here
fields = ('row_id', 'name', 'track_no', 'dev_type','checkbox')
sequence = fields
order_by = ('name', )
您可以自定义字段或添加新字段,文档解释得很好。创建 table 后,您需要在视图中加载 table:
from django_tables2 import RequestConfig
from tables import DeviceTable
def yourView(request, ...):
# ... Your actual code ...
# We get the object list
device_list = Device.objects.all()
# We pass the object list to the table
table = DeviceTable(device_list)
# RequestConfig is used to automatically add pagination to the table
RequestConfig(request, paginate={"per_page": 10}).configure(table)
return render_to_response('your_template.html', {'table': table, }, context_instance=RequestContext(request))
要在您的模板中呈现此 table,您需要加载 template_tag 以呈现 table:
{% load render_table from django_tables2 %}
# ... Your other code ...
<div class="col-md-12">
{% render_table table %}
</div>