Django:如何使用类似href =& quot; file:///C:/path/file.html& quot;的链接直接在浏览器中打开本地html文件 [英] Django: how to open local html files directly in the browser with links like href="file:///C:/path/file.html"

查看:132
本文介绍了Django:如何使用类似href =& quot; file:///C:/path/file.html& quot;的链接直接在浏览器中打开本地html文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在制作一个django应用程序来索引我保存在不同分区和目录中的本地文件(html,文本,pdf等)的集合,以便我可以轻松地根据名称,日期,标题,文件的等.就像一个高级定位,unix实用程序.它会生成一个包含文件链接的动态页面,如果是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>

问题是,当我单击它时,它什么也没有做,甚至没有错误消息.如果我保存此生成的html页面并直接在浏览器中打开它,则效果很好.我认为它不适用于安全性问题,但我不假装将其用作Internet上的Web应用程序,而是用作本地应用程序.我正在使用django开发服务器.我知道django可以提供将静态文件放在特定目录中的服务,但这不是我所需要的(文件位于多个位置);我想将浏览器中的文件作为本地文件而不是通过服务器加载.能做到吗?

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?

在django中,有没有一种方法可以使"file:///C:/path/file.html"方案在生成的动态页面中起作用?

Is there a way in django to make the "file:///C:/path/file.html" scheme work in the generated dynamic pages?

推荐答案

该问题与django无关,但与浏览器(在我的情况下为firefox 4)无关.出于安全原因,Firefox不允许从远程站点链接到本地​​文件.我必须禁用此http://localhost:8000的安全检查,并且它可以正常工作.由于更改仅影响本地主机,因此不应该是安全问题.此链接说明了如何执行此操作:

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 基本上,您需要做的就是使用以下命令在您的firefox配置文件文件夹中创建一个user.js文件:

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:

user_pref("capability.policy.policynames", "localfilelinks");
user_pref("capability.policy.localfilelinks.sites", "http://localhost:8000");
user_pref("capability.policy.localfilelinks.checkloaduri.enabled", "allAccess");

如果使用noscript,还必须更改一些配置:NoScript选项(高级->受信任->允许本地链接").

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.

这篇关于Django:如何使用类似href =&amp; quot; file:///C:/path/file.html&amp; quot;的链接直接在浏览器中打开本地html文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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