Django:如何使用 href=“file:///C:/path/file.html” 等链接直接在浏览器中打开本地 html 文件
我正在制作一个 django 应用程序来索引我保存在不同分区和目录中的本地文件集合(html、文本、pdf 等),以便我可以根据文件的名称、日期、标题等轻松搜索文件。它就像一个高级定位,unix 实用程序。它生成一个带有文件链接的动态页面,对于 html 文件,我应该单击并在浏览器中加载本地文件。生成的页面包含以下链接:
问题是,当我单击它时,它什么也不做,甚至没有错误消息。如果我保存这个生成的 html 页面并直接在浏览器中打开它,它就可以正常工作。我认为它不适用于安全问题,但我不会假装将其用作互联网上的网络应用程序,而是将其用作本地应用程序。我正在使用 django 开发服务器。我知道 django 可以提供静态文件,将它们放在特定的目录中,但这不是我需要的(文件位于多个位置);我想将浏览器中的文件作为本地文件加载,而不是通过服务器。这可以做到吗?
django 有没有办法使“file:///C:/path/file.html”方案在生成的动态页面中工作?
I'm making a django app to index my collection of local files (html, text, pdf, ... ) that I keep in diferent partitions and directories so I can search easily based on the name, date, title, etc of the files. It's like a advance locate, the unix utility. It generates a dynamic page with links for the files and in the case of the html files I should click and load the local file in the brower. The generated page contains links like:
<a href="file:///C:/somepath/mylocalfile.html">Title of local file</a></li>
The problem is that when I click it does nothing, not even error messages. If I save this generated html page and open it in the brower directly it works fine. I think it doesn't work for security issues but I do not pretend to use it as a web app over the internet but as a local app. I am using the django development server. I know that django can serve static files putting them in a specific directory but this isn't what I need (the files are in multiple locations); I want to load the files in the browser as local files, not through the server. Can this be done?
Is there a way in django to make the "file:///C:/path/file.html" scheme work in the generated dynamic pages?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这个问题与 django 无关,而是与浏览器有关(在我的例子中是 firefox 4)。出于安全原因,Firefox 不允许从远程站点链接到本地文件。我必须禁用
http://localhost:8000
的安全检查,它起作用了。由于更改仅影响本地主机,因此不应成为安全问题。此链接解释了如何执行此操作:http: //kb.mozillazine.org/Links_to_local_pages_don%27t_work
基本上,您需要做的就是在您的 Firefox 配置文件文件夹中创建一个
user.js
文件:如果您使用 noscript,您还必须更改一些配置:NoScript 选项(“高级 -> 受信任 - >“允许本地链接”)。
其他浏览器可能有此安全检查,因此您必须根据您使用的操作系统和浏览器进行不同的更改。
The problem had nothing to do with django but the browser (in my case firefox 4). Firefox doesn't allow to link to local files from remote sites for security reasons. I have to disable this security check for
http://localhost:8000
and it worked. As the change only affects localhost it shouldn't be a security issue.This link explain how to do it:http://kb.mozillazine.org/Links_to_local_pages_don%27t_work
Basically all you need to do is create a
user.js
file in your firefox profile folder with this:If you use noscript you also have to change some configuration: NoScript Options ("Advanced -> Trusted -> "Allow local links").
Other browsers may have this security checks so you will have to do diferent changes accoding to the operating system and browser you use.
:
是错误的。请使用|
代替。:
is wrong. Use|
instead.