【问题标题】:Link column with link to a file in static (django-tables2, Django)链接列与静态文件的链接(django-tables2,Django)
【发布时间】:2018-04-20 17:58:36
【问题描述】:

我在 Django 中使用 django-tables 生成一个表。 我想在我的静态目录中创建一个包含 txt 文件链接的列。 当用户点击链接时,应该会显示txt文件。

要在 html 中创建指向 txt 文件的链接,我只需这样做:

<a href="{% static co.log %}">txtfile</a>

但是,我在使用 django-tables 找到正确的方法时遇到了问题。 我尝试将链接列定义如下:

logfiles = tables.LinkColumn('{static', text='txtfile', args=[A('log')], orderable=False, empty_values=())

这给出了错误 “未找到 '{static' 的反向。'{static' 不是有效的视图函数或模式名称。”

我也试过这个:

tables.py

logfiles = tables.LinkColumn('logfile', text='bla', orderable=False, empty_values=())

urls.py:

url(r'^logfile/', views.logfile, name='logfile')

views.py:

def logfile(request):
return HttpResponse('<p>yeah</p>')

所以我可以找到一种方法来打开一个新的 url,但是如何打开一个特定的静态文件,即如何从 [A('log')] 传递信息,这基本上是文件名?

感谢任何帮助。

【问题讨论】:

    标签: django django-tables2


    【解决方案1】:

    您可以使用TemplateColumn 来实现:

    class LogTable(tables.Table):
        log = tables.TemplateColumn(
            template_code='{% load static %}<a href="{% static value %}">txtfile</a>'
        )
    

    注意列名是log,所以不需要指定访问者。如果您希望颜色以不同的名称显示,请使用 verbose_name kwarg。

    【讨论】:

      猜你喜欢
      • 2023-04-03
      • 2019-06-04
      • 2013-04-06
      • 2019-05-28
      • 1970-01-01
      • 2020-12-09
      • 1970-01-01
      • 1970-01-01
      • 2012-01-26
      相关资源
      最近更新 更多