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

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

问题描述

我正在制作一个django应用程序来索引我的本地文件(html,text,pdf,...)的集合,我保存在不同的分区和目录中,以便我可以根据名称,日期,标题,文件等。这就像一个高级定位,unix实用程序。它生成一个包含文件链接的动态页面,在html文件的情况下,我应该单击并加载浏览器中的本地文件。生成的页面包含如下链接:



< a href =file:/// C:/somepath/mylocalfile.html>本地文件的标题< / a>< / li>



问题是当我点击它什么也不做,甚至错误消息。如果我保存这个生成的html页面,并直接在浏览器中打开它,工作正常。我认为它不适用于安全问题,但我不假装将其用作互联网上的网络应用程序,而是作为本地应用程序。我正在使用django开发服务器。我知道django可以将静态文件放在特定的目录中,但这不是我需要的(文件在多个位置);我想将浏览器中的文件作为本地文件加载,而不是通过服务器加载。可以这样做吗?



有没有办法在django中使file:/// C:/path/file.html方案在生成的动态页面?

解决方案

该问题与django无关,但浏览器(在我的例子中为firefox 4)。出于安全考虑,Firefox不允许从远程站点链接到本地​​文件。我必须禁用这个安全检查 http:// localhost:8000 ,它的工作。由于更改只影响本地主机,它不应该是一个安全问题。此链接说明了如何做:



http://kb.mozillazine.org/Links_to_local_pages_don%27t_work
基本上所有你需要做的是创建一个用户您的firefox配置文件文件夹中的.js 文件:

  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选项(高级 - >可信 - >允许本地链接)。



其他浏览器可能会进行此安全检查,因此您必须根据您使用的操作系统和浏览器进行不同的更改。 p>

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?

解决方案

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:

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

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:如何直接在浏览器中打开本地html文件,链接如href =“file:/// C:/path/file.html”;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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