链接列,带有指向静态文件的链接(django-tables2,Django) [英] Link column with link to a file in static (django-tables2, Django)
问题描述
我在Django中生成带有django-tables的表.我想创建一列,其中包含指向静态目录中txt文件的链接.当用户单击链接时,将显示txt文件.
I generate a table with django-tables within Django. I want to create a column with links to txt files in my static directory. When the user clicks on the link, the txt file should be displayed.
要在html中创建指向txt文件的链接,只需执行以下操作:
To create a link to the txt file within an html, I simply do:
<a href="{% static co.log %}">txtfile</a>
但是,我在使用django-tables找到正确的方法时遇到了问题.我试图按如下方式定义链接列:
However, I have problems finding the right way to do this using django-tables. I tried to define the link column as follows:
logfiles = tables.LinkColumn('{static', text='txtfile', args=[A('log')], orderable=False, empty_values=())
这给出了错误找不到'{static'的反向字符.'{static'不是有效的视图函数或模式名称."
This gives the error "Reverse for '{static' not found. '{static' is not a valid view function or pattern name."
我也尝试过:
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')]中传递信息,该信息基本上是文件名?
So I can find a way to open a new url, but how to open a specific static file, i.e.how to pass the info from [A('log')], which is basically the filename?
感谢您的帮助.
推荐答案
You could use a TemplateColumn
to achieve this:
class LogTable(tables.Table):
log = tables.TemplateColumn(
template_code='{% load static %}<a href="{% static value %}">txtfile</a>'
)
请注意,列名是 log
,因此无需指定访问器.如果您希望颜色以其他名称显示,请使用 verbose_name
kwarg.
Note that the column name is log
, so there is no need to specify the accessor. If you want the color to appear with a different name, use the verbose_name
kwarg.
这篇关于链接列,带有指向静态文件的链接(django-tables2,Django)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!