Django_tables2:根据请求动态隐藏列 [英] Django_tables2: Dynamically hiding columns based on the request

查看:113
本文介绍了Django_tables2:根据请求动态隐藏列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个基于具有多个字段的模型的表。我也有两个 TemplateColumn s,一个用于编辑特定实体,另一个用于删除实体。这是我的代码:

I have a table that's based on a model that has several fields. I also have two TemplateColumns, one for editing the specific entity and another for deleting it. Here is my code:

class EntitetTable(tables.Table):
    edit = tables.TemplateColumn(template_name='azuriranje/izmena.html',
                            orderable=False, visible=False)
    delete = tables.TemplateColumn(template_name='azuriranje/brisanje.html',
                            orderable=False, visible=False)

    class Meta:
        abstract = True
        attrs = {'class': 'paleblue', }

class TipPredmetaTable(EntitetTable):
    class Meta(EntitetTable.Meta):
        model = models.TipPredmeta

现在,我的系统中有一个用户层次结构,只有作为簿记员的用户才能编辑和删除数据。话虽这么说,我试图在我的视图中实施检查以隐藏两个 TemplateColumn s:

Now, I have a user hierarchy in my system and only users that are bookkeepers can edit and delete the data. That being said, I tried to implement a check in my view to hide the two TemplateColumns:

@login_required
def tippredmeta(request):
    try:
        korisnik = request.user.radnik
    except ObjectDoesNotExist:
        return HttpResponseRedirect("/main/")
    queryset = TipPredmeta.objects.all()
    table = TipPredmetaTable(queryset)
    if korisnik.is_kustos:
        table.edit.visible = True
        table.delete.visible = True
    RequestConfig(request).configure(table)
    return render_to_response('azuriranje/tabelaPrikaz.html', {'table': table, },
                              context_instance=RequestContext(request))

但是,我在<$ c $上遇到以下异常c> table.edit.visible = True 行:

Exception Type:  AttributeError
Exception Value: 'TipPredmetaTable' object has no attribute 'edit'

现在,这是我尝试过的事情:
-首先我考虑过使用字段并排除,但是我无法动态更改。
-然后我考虑将所有这些放入 __ init __ 方法中,有效地编辑和删除我的EntitetTabel的属性(目的是解决错误),但是错误消失了,我的TemplateColumns也消失了。我尝试通过字段显示它们,但这没有帮助。我的猜测是,超类table.Table不能这样工作。

Now, here are the things I've tried: - First I thought about using fields and exclude, but I couldn't alter that dynamically. - Then I thought about placing all of this into the __init__ method, effectively making edit and delete attributes of my EntitetTabel (the idea was to solve the error) but while the error was gone, so were my TemplateColumns. I tried displaying them via fields, but that didn't help. My guess is that the superclass, the tables.Table, doesn't work like that.

推荐答案

您可以使用<$ tables.Table 的c $ c> exclude 属性用于排除不需要的字段。创建表实例后,它也可以工作。因此,根据您的情况,您可以执行以下操作。

You can use the exclude attribute of the tables.Table for excluding unwanted fields. It also works after creating the table instance. So in your case, you can do something like this.

首先使您的列可见(我删除了 visible = False ):

First leave your columns visible (i removed the visible=False):

class EntitetTable(tables.Table):
    edit = tables.TemplateColumn(template_name='azuriranje/izmena.html',
                                 orderable=False)
    delete = tables.TemplateColumn(template_name='azuriranje/brisanje.html',
                                 orderable=False)

然后,根据您的条件,您可以排除编辑和删除字段:

Then based on your condition, you can exclude the edit and delete fields:

table = TipPredmetaTable(queryset)
if not korisnik.is_kustos:
    table.exclude = ('edit', 'delete',) 

这篇关于Django_tables2:根据请求动态隐藏列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆