Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
269 views
in Technique[技术] by (71.8m points)

Link column with link to a file in static (django-tables2, Django)

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.

To create a link to the txt file within an html, I simply do:

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

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=())

This gives the error "Reverse for '{static' not found. '{static' is not a valid view function or pattern name."

I also tried this:

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>')

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?

Any help is appreciated.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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>'
    )

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...