4

如题,我有表格

file = forms.FileFIeld(widget = forms.FIleInput())

但这允许我浏览客户端计算机上的文件。我想向我展示我自己的服务器端文件。我完全了解安全风险。这是我的个人项目,不会被其他任何人使用(以防有人对我大喊大叫)。它不必只是相同的机制。我只想从表单中获取这个名字。不得上传或下载任何内容。

这将作为另一个服务器应用程序的文件选择。

如果无法浏览整个计算机,如何指定可以存储文件以供进一步浏览的目录?

4

2 回答 2

2

如果你想建立一个系统来远程访问你计算机上的文件,你可以使用 ftp 或 ssh 来完成。

如果您需要访问特定目录中的文件,您可以将它们放在 django 的static一部分,并让django 为您提供静态内容。然而,这不是 django 的预期设计,您不妨使用 Apache 提供 http 服务器的文件。

如果你正在寻找构建一个 google docs/dropbox 类型的网络服务,那么 django 可以作为一个网络框架来帮助你。但是,您需要运行某种本地索引并使用PyLucene之类的东西将所有文件元数据索引添加到您的数据库中,然后将相同的文件上传到在线或您的服务文件夹中的可下载路径。static这本身不是 Django 问题。

于 2012-10-01T17:21:07.990 回答
0

您可以通过模型浏览存储的文件。

假设你的模型是这样声明的

import os
class Document(models.Model):
    name = models.CharField(max_length=64)
    doc_file = models.FileField(upload_to='documents')

您可以像任何其他字段一样获取文件

names = []
# To browse your saved file, get the containing models
documents = Document.objects.all()
for doc in documents:
    # This is how you get the URL of the file field
    url = doc.doc_file.url
    # If you need the path of the stored file (in the server)
    doc_path = doc.doc_file.path
    # get name and size of the file
    name, size = doc.doc_file.name.split(os.path.sep)[-1], doc.doc_file.size
    names.append(name)
#process your file names
...

然后,您只需处理 URL,例如,将其显示给您的用户。

于 2012-10-01T16:59:42.460 回答