python - Django Django_Tables2 字段

标签 python django django-tables2

我只想选择 Django_Table2 元素中的几个字段。我一直在看django_table2网站django_table2我找不到太多关于如何限制 django_table2 元素中使用的字段数量的信息。这是我的代码。

这是我的项目 View .py:

from django.shortcuts import get_object_or_404, render
from django.http import HttpResponseRedirect
from django.core.urlresolvers import reverse
from django.views import generic
from django.utils import timezone

from .models import Project

class IndexView(generic.ListView):
    model = Project.objects.values("id","name","jobNumber", "contractor", "assigned_to", "created_date")
    template_name = 'project/index.html'

    def get_queryset(self):
        return Project.objects.values("id","name","jobNumber", "contractor", "assigned_to", "created_date")

class DetailView(generic.DetailView):
    model = Project
    template_name = 'project/detail.html'

这是我的index.html 模板:

{% load render_table from django_tables2 %}
{% block content %}
    <h1>Projects</h1>
    <ul>
    {% for project in project_list %}
        <li><a href="{% url 'project:detail' project.id %}">{{ project.name }}</a></li>
    {% endfor %}

    </ul>
    {% render_table project_list %}
{% endblock %}

如何限制 django_tables2 显示的字段/列的数量?

最佳答案

tutorial说“虽然简单,但将查询集直接传递给 {% render_table %} 不允许进行任何自定义。为此,您必须定义一个 Table 类。” API显示 Table.Meta 有字段并排除属性,类似于 ModelForms。

关于python - Django Django_Tables2 字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36476822/

相关文章:

python - 在for循环中按字典值分组

mysql - 避免 Django 中自定义 get_or_create 的竞争条件?

python - 在大型表上使用 Django-Filter 以及 DataTables2

python - 将 django-tables 的列自定义为下拉列表

python - Django Tables 2 字段访问器向后关系

python - 使用 Python 连接到 Oracle AQ

python - 替换字符串中多个字符的最有效方法

python - 将图像文件作为图层加载到 Photoshop

python - 在 Django 中通过 IP 地址进行身份验证

python - 在 Python 上编写计算器