本文介绍了在Django的TEMPLATE_DIRS中使用外部URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Settings.py中Django的TEMPLATE_DIRS要求使用unix样式斜杠。



因此,当我打电话时

  get_template('some / template.html')

在视图中,结果始终从根开始,并导致对

  / home / username / projectname / public的调用/some/template.html 

问题是我想使用完全托管在模板上的模板不同的网站。



给出一个http路径,


$ p $ b

>

  TEMPLATE_DIRS('http://example.com/',)

在Settings.py中会强制

  get_template('some / template .html')

以便尝试查找

  /home/username/projectname/public/http://example.com/some/template.html 

我试图这样规避

  TEMPLATE_DIRS( '../../../../http://example.com/',)

但是它仍然强制使用斜线,所以我得到了 /http://example.com,这是没有用的。



我的问题:

  1. 是否有办法欺骗它从另一台服务器的
    中提取模板文件?

  2. 鉴于视图需要对模板文件进行
    处理,这甚至可行吗?

  3. 是否可以创建不需要unix斜杠的'django.template.loaders.filesystem.Loader'替代项?


解决方案

如果您不想使用模板目录,则无需使用。如果您有一台正在提供模板文件的服务器,则只需使用 urllib2 远程获取它们,然后手动创建并渲染带有上下文的模板:

 从django.template导入urllib2 
导入上下文,模板

tpl_html = urllib2.urlopen( http:// mysite.com)
tpl =模板(tpl_html)
返回tpl.render(Context({
'some_variable':'some_val',
})

如果要执行此操作,则必须合并一些缓存,因为使用该模板的每个请求都需要发出外部请求。或者,您也可以将其写入自定义加载程序,但会受到相同的限制。


Django's TEMPLATE_DIRS in Settings.py calls for unix style slashes.

Because of this, when I call

get_template('some/template.html')

in a view, the result always starts at the root, and results in a call to

/home/username/projectname/public/some/template.html

The problem is that I'd like to use templates hosted on an entirely different site. This works fine for other Settings.py fields (MEDIA_URL and STATIC_URL), where it will take an absolute http path with no objection.

Given an http path,

 TEMPLATE_DIRS ('http://example.com/',)

in Settings.py will force

get_template('some/template.html')

in a view to try and find

/home/username/projectname/public/http://example.com/some/template.html

I've tried to circumvent this like so

TEMPLATE_DIRS ('../../../../http://example.com/',)

But it still forces a leading slash, so I get "/http://example.com", which is useless.

My questions:

  1. Is there a way to trick this into pulling the template files fromanother server?
  2. Is that even feasible, given that the template files need to beprocessed for the view?
  3. Is it possible to create an alternate to 'django.template.loaders.filesystem.Loader' that doesn't call for unix style slashes?
解决方案

You don't need to use the template directory is you dont want to. If you have a server that is serving template files, you can simply fetch them remotely using urllib2 and create and render the template with a context manually:

import urllib2
from django.template import Context, Template

tpl_html = urllib2.urlopen("http://mysite.com")
tpl = Template(tpl_html)
return tpl.render(Context({
    'some_variable' : 'some_val',
})

If you are going to do this, you have to incorporate some caching, as for every request to using this template, you need to make an external request. Alternatively you could write this into a custom loader but it will suffer the same limitations.

这篇关于在Django的TEMPLATE_DIRS中使用外部URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-26 03:23