Django:包含外部包含文件 [英] Django: include external include files

查看:140
本文介绍了Django:包含外部包含文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我建立一个网络应用程序,它将成为现有静态网站的一部分。我更喜欢使用当前站点的页眉和页脚,它们是静态的.inc包含文件。



有没有办法包含这些文件,如: p>

{%include'http://www.mysote.com/inc/footer.inc'%}

解决方案

在Django中没有内置的方法,但它是一个非常简单的模板标签,可以自己编写(有一个很好的机会,有人已经写了这样一个事情,虽然快速搜索没有为我打开)。如果你想去那条路线,你可以通过一个快速的 simple_tag (这里记载: https://docs.djangoproject.com/en/1.3/howto/custom-template-tags/#shortcut-换简单标记)。它可能是这样简单的:

  def include_external(url):
import urllib2
return urllib2.urlopen(url).read()

register.simple_tag(include_external)


{%include_external'http:// ....' %}

然而,正如Umang所说,这可能是有问题的 - 提取包含文件可能会显着增加页面加载时间,您将保证您的静态网站的失败也会导致您的Django应用程序失效。如果任何一个事情都被证明是一个问题,你可以看看缓存的标题 - 但是,这增加了额外的复杂性,你可能会更好的只是复制你的头文件,每次更新。


Im building a web app which will be part of an existing static website. I'd prefer to use the header and footer from the current site which are static .inc include files.

Is there a way to include these files something like:

{% include 'http://www.mysote.com/inc/footer.inc' %}

解决方案

There isn't a built-in way to do this in Django, but it would be a really easy template tag to write on your own (there's a decent chance someone has already written such a thing, though a quick search didn't turn it up for me). If you want to go that route, you can do that with a quick simple_tag (documented here: https://docs.djangoproject.com/en/1.3/howto/custom-template-tags/#shortcut-for-simple-tags). It could probably be as simple as something like:

def include_external(url):
    import urllib2
    return urllib2.urlopen(url).read()

register.simple_tag(include_external)


{% include_external 'http://....' %}

However, as Umang mentioned, that is potentially problematic--fetching that include file will probably significantly increase your page load time, and you'll guarantee that a failure in your static site will bring down your Django app as well. If either of those things turns out do be a concern, you could look at caching the header--however, that's adding additional complexity, and you might be better of just copying your header file over each time it's updated.

这篇关于Django:包含外部包含文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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