仅允许超级用户调用模板列-Django

时间:2019-08-14 08:40:42

标签: django django-templates

我已经使用Django表开发了一个模板列,但我希望只有超级用户才能查看该列。

MyTables.py

class DeviceTable(tables.Table):
    def view(request):
        if request.user.is_superuser:
            edit = tables.TemplateColumn(template_code)

    class Meta:
        attrs = {"class": "table table-striped table-hover"}
        model = Devices
        fields = (
            "name",
            "location",
            "phone_number",
            "ip_address",
            "created_date",

        )

上面的代码不起作用。

1 个答案:

答案 0 :(得分:2)

在这种情况下,您可以使用before_render钩子:

class DeviceTable(tables.Table):
    edit = tables.TemplateColumn(template_code)

    class Meta:
        attrs = {"class": "table table-striped table-hover"}
        model = Devices
        fields = (
            "name",
            "location",
            "phone_number",
            "ip_address",
            "created_date",

        )

    def before_render(self, request):
        if request.user.is_superuser:
            self.columns.show('edit')
        else:
            self.columns.hide('edit')