Django: include external include files

一个人想着一个人 提交于 2019-12-24 06:20:29

问题


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' %}


回答1:


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.



来源:https://stackoverflow.com/questions/7333843/django-include-external-include-files

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!