标签: django-tables2

Django-tables2没有排序

我使用django-tables2显示数据库表.全部显示正常,但单击列标题不会按该列排序.标题是可点击的,但在它们的html中没有url文本,例如:

<div class="table-container">
<table class="paleblue"><thead><tr><th class="id orderable sortable"><a href="">ID</a>
</th><th class="orderable sortable system"><a href="">System</a></th> ...
Run Code Online (Sandbox Code Playgroud)

我检查了django-tables2模板源代码,它是这样的:

... <a href="{% querystring table.prefixed_order_by_field=column.order_by_alias.next %}"> ...
Run Code Online (Sandbox Code Playgroud)

哪个我不明白.

我只能通过在view.py中设置order_by来进行排序工作:

class ResultsTable(tables.Table):
    class Meta:
        model = Performance
        attrs = {'class': 'paleblue'}
        order_by_field = True

def result(request, system):
    results = Performance.objects.filter(system__name=system)
    table = ResultsTable(results, order_by=('<any column name from Performance>',))
    RequestConfig(request).configure(table)
    return render_to_response('result.html', {'table': table})
Run Code Online (Sandbox Code Playgroud)

但这显然只适用于一列,我想点击列来选择要排序的列.

文档中我的理解是,按列排序应该"开箱即用",这是正确的,还是我做错了什么?

django django-tables2

2
推荐指数
1
解决办法
2466
查看次数

如何在django块中包含模板标记

我使用django_tables2(http://django-tables2.readthedocs.org/en/latest/)进行HTML表格渲染,但我的模板无法工作:

{% extends 'base.html' %}
{% block main %}
    {% render_table table %} 
{% endblock %}
Run Code Online (Sandbox Code Playgroud)

错误消息如下:

Invalid block tag: 'render_table', expected 'endblock'
Run Code Online (Sandbox Code Playgroud)

是否有上述tables2插件的替代品?

django django-templates django-tables2

2
推荐指数
1
解决办法
1443
查看次数

在django-tables2中有很多人

通过ForeignKey我有django的模型有很多对:

    class A(m.Model):
        id = m.AutoField(primary_key=True)
        name = m.CharField(max_length=250, unique=True, null=False)

        def __str__(self):
            return self.name


    class B(m.Model):
        id = m.AutoField(primary_key=True)
        name = m.CharField(max_length=250, unique=True, null=False)
        a = m.ForeignKey(A)

        def __str__(self):
            return self.name


    class C(m.Model):
        id = m.IntegerField(null=False, unique=True, primary_key=True)
        name = m.CharField(max_length=250, unique=True, null=False)
        bs = m.ManyToManyField(B, through='D')

        def __str__(self):
            return '%d, %s, (%s), (%s)' % (
                self.id,
                self.name, 
                ', '.join(b.name for b in self.bs.all()), 
                ', '.join(b.a.name for b in self.bs.all()))
                )


    class D(m.Model):
        c = m.ForeignKey(C)
        b = m.ForeignKey(B) …
Run Code Online (Sandbox Code Playgroud)

python django many-to-many foreign-key-relationship django-tables2

2
推荐指数
1
解决办法
1436
查看次数

从 django_tables2 获取行数

我是 django 的新手,我需要获取在我的模板中使用 django table2 呈现的表格的表格行数(最好是在渲染表格之前)

有一些这样的代码:

{% load render_table from django_tables2 %} 
{% block content %}
    {% render_table  participations_table %}
{% endblock %}
Run Code Online (Sandbox Code Playgroud)

如果其中至少有 1 行,我想呈现此表。

django django-templates django-tables2

2
推荐指数
1
解决办法
2681
查看次数

使用 django-tables2 如何显示模型 @property?

@property我的模型中有一个,它基本上是模型中几个值的字符串组合。

  @property
    def mfg_link(self):
        return ''.join('http://mfg.com/model/' + str(self.model_numer))
Run Code Online (Sandbox Code Playgroud)

我可以将它添加mfg_linklist_display管理模型上并且它工作正常,但是当我将查询集传递给表时它没有显示在生成的表中,其他字段显示正常。

这似乎很明显,但不幸的是,几个小时的搜索并没有帮助。

非常感谢!

django-models django-tables2

2
推荐指数
1
解决办法
1773
查看次数

如何使用 django-tables2 连接两个表

任何人都可以提供一个清晰的示例,说明如何使用django-tables2从两个(或更多)相关模型中选择和呈现数据来显示表格?

我发现了很多关于这个的帖子,其中大多数都很老,而且没有一个真正有效的例子。

这些是我的模型:

class Person(models.Model):
    name = models.CharField(verbose_name="Name",max_length=50)
    fname = models.CharField(verbose_name="F.Name",max_length=50)

class Speech(models.Model):
    person = models.ForeignKey(Person, on_delete=models.CASCADE)
    said = models.CharField(verbose_name="Said",max_length=50)
Run Code Online (Sandbox Code Playgroud)

我只想显示一个包含“姓名、F.姓名、赛义德”列的表格。哪种方法最好?并且有多个表?

提前致谢。

join django-tables2

2
推荐指数
1
解决办法
1272
查看次数

单元格颜色 Django-Tables2

问题:我在哪里编辑我的 django 代码以根据业务逻辑更改单个单元格的背景颜色?

在我的 views.py 中,我有捕获列“pts”最大值的逻辑:

def show_teams(request):
reg = Teamoffense.objects.filter(~Q(rk='RK'))
pts = Teamoffense.objects.filter(~Q(pts='PTS')).values('pts')
seq = [item['pts'] for item in pts]
maxseq = max(seq)

table = SimpleTable(reg)
table_to_report = RequestConfig(request).configure(table)
if table_to_report:
    return create_report_http_response(table_to_report, request)
return render(request, 'index.html', {
    'table': table,
    'reg': reg,
    'maxseq': maxseq,
})
Run Code Online (Sandbox Code Playgroud)

如何在该列中呈现最大值为 bgcolor = 'green' 的任何单元格?我目前有一个简单的表格,显示如下:

class SimpleTable(TableReport):

class Meta:
    model = Teamoffense
    exclude = ("column1","column2")
    exclude_from_report = ("column1","column2")
    attrs = {'class': 'paleblue'}
Run Code Online (Sandbox Code Playgroud)

django django-templates django-views django-tables2

2
推荐指数
1
解决办法
1803
查看次数

Django表2创建自定义计算字段

def calculateTotalVists(days):
    total = 0
    for i in days:
        total += i
    return total

class ClientTable(tables.Table):
    class Meta:
        model = Client
        fields = ('id', 'name', 'phone')
        attrs = {'class': 'table table-striped table-bordered', 'id': 'clients'}
Run Code Online (Sandbox Code Playgroud)

客户端模型具有"days"字段,该字段是列表.我想创建一个自定义列,在天数传递到calculateTotalVists()之后显示结果.但是,我不是如何使用具有函数的访问器.

python django django-tables2 web

2
推荐指数
1
解决办法
695
查看次数

为 django-tables2 设置空文本行为

我的 django 表中的某些列碰巧是空的,因此在那里呈现的文本是“无”。我想看到一个空白区域。

django tables2 有一些关于这个主题的文档,但我不完全理解。我必须在哪里定义这个 empty_text 行为参数?在相关的类元中尝试过,但显然没有效果。

django-tables2

1
推荐指数
1
解决办法
2572
查看次数

如何保持用户登录django

我在应用程序中进行了登录。然后我转到列出数据库中所有用户的屏幕。当我选择一个特定的用户时,我丢失了已登录的用户。那么,我怎样才能让我的用户留在所有的应用程序中呢?

我的 views.py 列出了所有用户。此时,url 显示我的用户(http://localhost:8000/os/acesso/consultausuario/9

def acesso_consultausuario(request, id=None):

    instance = get_object_or_404(usuario, id=id)

    queryset = usuario.objects.all().order_by('dat_criacao_usuario')
    instancia_usuario_filter = usuariofilter(request.GET, queryset=queryset)

    table = usuariotable(instancia_usuario_filter.qs)

    RequestConfig(request, paginate={'per_page': 25}).configure(table)

    paginator = Paginator(instancia_usuario_filter.qs, 5)
    page = request.GET.get('page')

    try:
        response = paginator.page(page)
    except PageNotAnInteger:
        # if page is not an integer, deliver first page
        response = paginator.page(1)
    except EmptyPage:
        # if page is out of range, deliver last pages of results
        response = paginator.page(paginator.num_pages)

     context = {
        "instance": instance,
        "filter": instancia_usuario_filter,
        "response": response,
        "table": …
Run Code Online (Sandbox Code Playgroud)

python django django-filters django-tables2

1
推荐指数
1
解决办法
4256
查看次数