Is there a lower-level way to provide the list of loaders when rendering a template, as opposed to always having Django use the setting?
I’d like to use a custom template loader instance for only a few views (I have my reasons).
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
It looks like you’ll have to write some code of your own to do it. Let’s take a look at the normal code path for loading templates, if you use, say,
render_to_response, where the relevant part of the source is:That’s a call to
django.template.loader.render_to_string, which passes through some other functions and eventually ends up callingfind_template.The first time
find_templateis called, in initializes the globaltemplate_source_loaderscache based onsettings.TEMPLATE_LOADERS. So it looks like there’s no just extra argument you can pass in or anything like that.One possibility might be to add some loaders to
django.template.loader.template_source_loadersjust for the duration of that view. I don’t know if that will cause other problems; it feels dirty, but if it works, it’ll be pretty easy. (Just make a view decorator that does it.)If you don’t want to do that, it looks like you’ll have to replicate the work of
render_to_stringwith your own code (if you’re really sure you want to use per-view template loaders, which I’m accepting as a premise for the sake of this question but I bet isn’t actually necessary). There’s not all that much code there, and if you know in advance a specific loader and a single template name that you want to use, it’s actually pretty easy. (This is untested but will probably pretty much work.)If you want to support multiple possible loaders or a list of possible filenames, just copy the relevant code from django.template.loader.