Using external URLs in Django's TEMPLATE_DIRS

后端 未结 3 1484
野性不改
野性不改 2020-12-30 13:10

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

Because of this, when I call

get_template(\'some/template.html\')
3条回答
  •  醉梦人生
    2020-12-30 13:44

    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.

提交回复
热议问题