在Django模板中列出目录文件的内容 [英] List directory file contents in a Django template

查看:264
本文介绍了在Django模板中列出目录文件的内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习Python& Django的。 (感谢在这里做出贡献的每个人-这是非常宝贵的资源!)

I'm just learning Python & Django. (Thanks to everyone who contributes here -- it's been an invaluable resource!)

我遇到的一件看似基本的事情是呈现一个简单的静态文件列表(例如,服务器上单个存储库目录的内容)作为可下载链接的列表。这是否安全是另一个问题,但是假设我想这样做...

One seemingly basic thing that I'm having trouble with is rendering a simple list of static files (say the contents of a single repository directory on my server) as a list of downloadable links. Whether this is secure or not is another question, but suppose I want to do it...

这篇文章帮助我朝着正确的方向:
< a href = https://stackoverflow.com/questions/2360205/python-directory-list-returned-to-django-template> Python目录列表返回到Django模板

This post helped steer me in the right direction: Python directory list returned to Django template

如果从提示符处运行,此代码段将输出'myfiles'中的文件名:

This code snippet outputs the filenames in 'myfiles' if run from a prompt:

path= os.path.dirname(os.path.abspath(__file__))  
myfiles = os.path.join(path, 'myfiles')  
os.chdir(myfiles)  
for files in os.listdir("."):
     print files  

但是如何传递这些文件到Django模板并生成链接列表?我想我可能必须创建某种Python字典/元组以使其可迭代,将其作为模板变量传递并在for循环中呈现?

But how do I pass these files to a Django template and generate a list of links? I think I may have to create a Python dictionary/tuple of some sort to make it iterable, pass it as a template variable and render it in a for loop?

我必须在urls.py文件中添加一个条目才能使其工作?像这样吗

Do I have to add an entry to my urls.py file to make this work? Something like this?

(r'^myfiles/(?P<path>.*)$', 'django.views.static.serve', {'document_root': os.path.join(os.path.dirname(__file__), 'myfiles'), 'show_indexes': True}),

感谢您的帮助!我只是在学习,我真的无法从现有的在线代码片段中弄清楚如何做到这一点。

Thanks for your help! I'm just learning and I couldn't really figure out how to do this from existing code snippets online.

编辑:这就是我的看法.py :(您会从 \\中注意到我在Windows系统上)

This is what I've got in my views.py: (you'll notice from the '\\' that I'm on a Windows system)

def myfiles_page(request):
    path = os.path.dirname(os.path.abspath(__file__))
    myfiles = os.path.join(path, 'myfiles\\')
    os.chdir(myfiles)
    x = 0
    d = {}
    for file in os.listdir("."):
        d[x] = (myfiles + file)
        x = x + 1

    variables = RequestContext(request, {
    'user' : request.user,
    'filedict' : d,
    })
    return render_to_response('myfiles_page.html', variables)

尝试使其显示在模板中:
(根据 Django文档

This is how I was trying to get it to display in a template: (according to the Django documentation)

{% for key, value in filedict %}
    {{ key }}: {{ value }}
{% endfor %}

但是'filedict'仍未在模板中显示任何内容。有任何想法吗?

But 'filedict' is still not displaying anything in the template. Any ideas?

推荐答案

我认为有些事情并非如此。

Something very much not like that, I reckon.

您可能需要在views.py内部创建自己的函数-请参见教程。

You'll probably need to create a function of your own inside views.py - see tutorial.

该函数将从os.listdir获取文件列表

That function will get the file list from os.listdir into a list.

将上下文中的列表传递给模板,并使用您编写的模板进行render_to_response-请参见教程。

Pass that list in the context to a template and do render_to_response with a template you've written - see tutorial.

在模板中,其内容类似于:

In the template, its then something like:

{% for file in filelist %}
  {{ file }}
{% endfor %}

您可以在模板的上下文。您可能还希望传递带有URL的扩展文件列表-字典列表,其中每个字典都有一个名称和一个url。然后,您的模板为:

You can pass anything in the context to a template. You might want to pass an augmented list of files with URLs too - a list of dicts, where each dict has a name and a url. Then your template is:

{% for file in filelist %}
  <a href="{{file.url}}">{{ file.name }}</a>
{% endfor %}

大脂肪安全警告

不允许用户通过在URL中放置路径来浏览文件,至少在没有真正检查自己是否不能执行/myfiles/../../notmyfiles/之类的情况下, secret.txt并通过在路径中向上插入点来查看其他人的文件。

Don't let users browse files by letting them put a path in a URL, at least not without really really checking that they can't do stuff like /myfiles/../../notmyfiles/secret.txt and see other peoples files just by sticking dot-dots in the path to go upwards.

如果您不了解,则可能不应该编写代码除了玩具系统之外,您什么都不介意被撕成碎片!

If you dont understand that, then you probably shouldn't be coding this up on anything but a toy system that you don't mind being ripped to pieces!

这篇关于在Django模板中列出目录文件的内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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