{"title":"Upgrading templates to Django 1.8","version":"1.9","locale":"en","docname":"ref/templates/upgrading","url":"/en/1.9/ref/templates/upgrading/","canonical":"https://djangodocs.dev/en/1.9/ref/templates/upgrading/","summary":"Django’s template system was overhauled in Django 1.8 when it gained support for multiple template engines. This document complements the release notes with…","html":"<h1>Upgrading templates to Django 1.8<a class=\"heading-anchor\" href=\"#upgrading-templates-to-django-1-8\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h1>\n<p>Django’s template system was overhauled in Django 1.8 when it gained support\nfor multiple template engines. This document complements the <a class=\"reference internal\" href=\"/en/1.9/releases/1.8/\"><span class=\"doc\">release\nnotes</span></a> with detailed upgrade instructions on some topics.</p>\n<section id=\"the-templates-settings\">\n<h2>The <a class=\"reference internal\" href=\"/en/1.9/ref/settings/#std-setting-TEMPLATES\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">TEMPLATES</span></code></a> settings<a class=\"heading-anchor\" href=\"#the-templates-settings\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<p>A new setting was introduced in Django 1.8: <a class=\"reference internal\" href=\"/en/1.9/ref/settings/#std-setting-TEMPLATES\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">TEMPLATES</span></code></a>. All existing\ntemplate-related settings were deprecated.</p>\n<p>During the deprecation period, Django will create a backwards-compatible\n<a class=\"reference internal\" href=\"/en/1.9/ref/settings/#std-setting-TEMPLATES\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">TEMPLATES</span></code></a> based on the <code class=\"docutils literal notranslate\"><span class=\"pre\">TEMPLATE_*</span></code> settings if you don’t define\nit yourself.</p>\n<p>Here’s how to define <a class=\"reference internal\" href=\"/en/1.9/ref/settings/#std-setting-TEMPLATES\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">TEMPLATES</span></code></a> in your settings module.</p>\n<p>If you’re using the default value of <code class=\"docutils literal notranslate\"><span class=\"pre\">TEMPLATE_LOADERS</span></code>, that is, if it\nisn’t defined in your settings file or if it’s set to:</p>\n<div class=\"code-block\" data-language=\"default\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Code</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Code code\"><code><span class=\"p\">[</span><span class=\"s1\">&#39;django.template.loaders.filesystem.Loader&#39;</span><span class=\"p\">,</span>\n <span class=\"s1\">&#39;django.template.loaders.app_directories.Loader&#39;</span><span class=\"p\">]</span>\n</code></pre></div>\n<p>then you should define <a class=\"reference internal\" href=\"/en/1.9/ref/settings/#std-setting-TEMPLATES\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">TEMPLATES</span></code></a> as follows:</p>\n<div class=\"code-block\" data-language=\"default\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Code</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Code code\"><code><span class=\"n\">TEMPLATES</span> <span class=\"o\">=</span> <span class=\"p\">[</span>\n    <span class=\"p\">{</span>\n        <span class=\"s1\">&#39;BACKEND&#39;</span><span class=\"p\">:</span> <span class=\"s1\">&#39;django.template.backends.django.DjangoTemplates&#39;</span><span class=\"p\">,</span>\n        <span class=\"s1\">&#39;DIRS&#39;</span><span class=\"p\">:</span> <span class=\"p\">[</span>\n            <span class=\"c1\"># insert your TEMPLATE_DIRS here</span>\n        <span class=\"p\">],</span>\n        <span class=\"s1\">&#39;APP_DIRS&#39;</span><span class=\"p\">:</span> <span class=\"kc\">True</span><span class=\"p\">,</span>\n        <span class=\"s1\">&#39;OPTIONS&#39;</span><span class=\"p\">:</span> <span class=\"p\">{</span>\n            <span class=\"s1\">&#39;context_processors&#39;</span><span class=\"p\">:</span> <span class=\"p\">[</span>\n                <span class=\"c1\"># Insert your TEMPLATE_CONTEXT_PROCESSORS here or use this</span>\n                <span class=\"c1\"># list if you haven&#39;t customized them:</span>\n                <span class=\"s1\">&#39;django.contrib.auth.context_processors.auth&#39;</span><span class=\"p\">,</span>\n                <span class=\"s1\">&#39;django.template.context_processors.debug&#39;</span><span class=\"p\">,</span>\n                <span class=\"s1\">&#39;django.template.context_processors.i18n&#39;</span><span class=\"p\">,</span>\n                <span class=\"s1\">&#39;django.template.context_processors.media&#39;</span><span class=\"p\">,</span>\n                <span class=\"s1\">&#39;django.template.context_processors.static&#39;</span><span class=\"p\">,</span>\n                <span class=\"s1\">&#39;django.template.context_processors.tz&#39;</span><span class=\"p\">,</span>\n                <span class=\"s1\">&#39;django.contrib.messages.context_processors.messages&#39;</span><span class=\"p\">,</span>\n            <span class=\"p\">],</span>\n        <span class=\"p\">},</span>\n    <span class=\"p\">},</span>\n<span class=\"p\">]</span>\n</code></pre></div>\n<p>If you aren’t using the default value of <code class=\"docutils literal notranslate\"><span class=\"pre\">TEMPLATE_LOADERS</span></code>, then you should\ndefine <a class=\"reference internal\" href=\"/en/1.9/ref/settings/#std-setting-TEMPLATES\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">TEMPLATES</span></code></a> as follows:</p>\n<div class=\"code-block\" data-language=\"default\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Code</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Code code\"><code><span class=\"n\">TEMPLATES</span> <span class=\"o\">=</span> <span class=\"p\">[</span>\n    <span class=\"p\">{</span>\n        <span class=\"s1\">&#39;BACKEND&#39;</span><span class=\"p\">:</span> <span class=\"s1\">&#39;django.template.backends.django.DjangoTemplates&#39;</span><span class=\"p\">,</span>\n        <span class=\"s1\">&#39;DIRS&#39;</span><span class=\"p\">:</span> <span class=\"p\">[</span>\n            <span class=\"c1\"># insert your TEMPLATE_DIRS here</span>\n        <span class=\"p\">],</span>\n        <span class=\"s1\">&#39;OPTIONS&#39;</span><span class=\"p\">:</span> <span class=\"p\">{</span>\n            <span class=\"s1\">&#39;context_processors&#39;</span><span class=\"p\">:</span> <span class=\"p\">[</span>\n                <span class=\"c1\"># Insert your TEMPLATE_CONTEXT_PROCESSORS here or use this</span>\n                <span class=\"c1\"># list if you haven&#39;t customized them:</span>\n                <span class=\"s1\">&#39;django.contrib.auth.context_processors.auth&#39;</span><span class=\"p\">,</span>\n                <span class=\"s1\">&#39;django.template.context_processors.debug&#39;</span><span class=\"p\">,</span>\n                <span class=\"s1\">&#39;django.template.context_processors.i18n&#39;</span><span class=\"p\">,</span>\n                <span class=\"s1\">&#39;django.template.context_processors.media&#39;</span><span class=\"p\">,</span>\n                <span class=\"s1\">&#39;django.template.context_processors.static&#39;</span><span class=\"p\">,</span>\n                <span class=\"s1\">&#39;django.template.context_processors.tz&#39;</span><span class=\"p\">,</span>\n                <span class=\"s1\">&#39;django.contrib.messages.context_processors.messages&#39;</span><span class=\"p\">,</span>\n            <span class=\"p\">],</span>\n            <span class=\"s1\">&#39;loaders&#39;</span><span class=\"p\">:</span> <span class=\"p\">[</span>\n                <span class=\"c1\"># insert your TEMPLATE_LOADERS here</span>\n            <span class=\"p\">]</span>\n        <span class=\"p\">},</span>\n    <span class=\"p\">},</span>\n<span class=\"p\">]</span>\n</code></pre></div>\n<p>Furthermore you should replace <code class=\"docutils literal notranslate\"><span class=\"pre\">django.core.context_processors</span></code> with\n<code class=\"docutils literal notranslate\"><span class=\"pre\">django.template.context_processors</span></code> in the names of context processors.</p>\n<p>If your settings module defines <code class=\"docutils literal notranslate\"><span class=\"pre\">ALLOWED_INCLUDE_ROOTS</span></code> or\n<code class=\"docutils literal notranslate\"><span class=\"pre\">TEMPLATE_STRING_IF_INVALID</span></code>, include their values under the\n<code class=\"docutils literal notranslate\"><span class=\"pre\">'allowed_include_roots'</span></code> and <code class=\"docutils literal notranslate\"><span class=\"pre\">'string_if_invalid'</span></code> keys in the\n<code class=\"docutils literal notranslate\"><span class=\"pre\">'OPTIONS'</span></code> dictionary.</p>\n<p>If it sets <code class=\"docutils literal notranslate\"><span class=\"pre\">TEMPLATE_DEBUG</span></code> to a value that differs from <a class=\"reference internal\" href=\"/en/1.9/ref/settings/#std-setting-DEBUG\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">DEBUG</span></code></a>,\ninclude that value under the <code class=\"docutils literal notranslate\"><span class=\"pre\">'debug'</span></code> key in <code class=\"docutils literal notranslate\"><span class=\"pre\">'OPTIONS'</span></code>.</p>\n<p>Once you have defined <a class=\"reference internal\" href=\"/en/1.9/ref/settings/#std-setting-TEMPLATES\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">TEMPLATES</span></code></a>, you can safely remove\n<code class=\"docutils literal notranslate\"><span class=\"pre\">ALLOWED_INCLUDE_ROOTS</span></code>, <code class=\"docutils literal notranslate\"><span class=\"pre\">TEMPLATE_CONTEXT_PROCESSORS</span></code>,\n<code class=\"docutils literal notranslate\"><span class=\"pre\">TEMPLATE_DEBUG</span></code>, <code class=\"docutils literal notranslate\"><span class=\"pre\">TEMPLATE_DIRS</span></code>, <code class=\"docutils literal notranslate\"><span class=\"pre\">TEMPLATE_LOADERS</span></code>, and\n<code class=\"docutils literal notranslate\"><span class=\"pre\">TEMPLATE_STRING_IF_INVALID</span></code>.</p>\n<p>If you are overriding some of these settings in tests, you should override the\nentire <a class=\"reference internal\" href=\"/en/1.9/ref/settings/#std-setting-TEMPLATES\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">TEMPLATES</span></code></a> setting instead.</p>\n</section>\n<section id=\"django-template-loader\">\n<h2><a class=\"reference internal\" href=\"/en/1.9/topics/templates/#module-django.template.loader\" title=\"django.template.loader\"><code class=\"xref py py-mod docutils literal notranslate\"><span class=\"pre\">django.template.loader</span></code></a><a class=\"heading-anchor\" href=\"#django-template-loader\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<section id=\"get-template-and-select-template\">\n<span id=\"get-template-upgrade-django-18\"></span><h3><a class=\"reference internal\" href=\"/en/1.9/topics/templates/#django.template.loader.get_template\" title=\"django.template.loader.get_template\"><code class=\"xref py py-func docutils literal notranslate\"><span class=\"pre\">get_template()</span></code></a> and <a class=\"reference internal\" href=\"/en/1.9/topics/templates/#django.template.loader.select_template\" title=\"django.template.loader.select_template\"><code class=\"xref py py-func docutils literal notranslate\"><span class=\"pre\">select_template()</span></code></a><a class=\"heading-anchor\" href=\"#get-template-and-select-template\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>In Django 1.8 <a class=\"reference internal\" href=\"/en/1.9/topics/templates/#django.template.loader.get_template\" title=\"django.template.loader.get_template\"><code class=\"xref py py-func docutils literal notranslate\"><span class=\"pre\">get_template()</span></code></a> and\n<a class=\"reference internal\" href=\"/en/1.9/topics/templates/#django.template.loader.select_template\" title=\"django.template.loader.select_template\"><code class=\"xref py py-func docutils literal notranslate\"><span class=\"pre\">select_template()</span></code></a> return a backend-dependent\n<code class=\"docutils literal notranslate\"><span class=\"pre\">Template</span></code> instead of a <a class=\"reference internal\" href=\"/en/1.9/ref/templates/api/#django.template.Template\" title=\"django.template.Template\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">django.template.Template</span></code></a>.</p>\n<p>For example, if <a class=\"reference internal\" href=\"/en/1.9/topics/templates/#django.template.loader.get_template\" title=\"django.template.loader.get_template\"><code class=\"xref py py-func docutils literal notranslate\"><span class=\"pre\">get_template()</span></code></a> loads a template\nwith a <a class=\"reference internal\" href=\"/en/1.9/topics/templates/#django.template.backends.django.DjangoTemplates\" title=\"django.template.backends.django.DjangoTemplates\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">DjangoTemplates</span></code></a> backend, then\nit returns a <code class=\"docutils literal notranslate\"><span class=\"pre\">django.template.backends.django.Template</span></code>.</p>\n<p><code class=\"docutils literal notranslate\"><span class=\"pre\">Template</span></code> objects must provide a\n<a class=\"reference internal\" href=\"/en/1.9/topics/templates/#django.template.backends.base.Template.render\" title=\"django.template.backends.base.Template.render\"><code class=\"xref py py-meth docutils literal notranslate\"><span class=\"pre\">render()</span></code></a> method whose signature\ndiffers slightly from the Django template language’s\n<a class=\"reference internal\" href=\"/en/1.9/ref/templates/api/#django.template.Template.render\" title=\"django.template.Template.render\"><code class=\"xref py py-meth docutils literal notranslate\"><span class=\"pre\">render()</span></code></a>.</p>\n<p>Instead of:</p>\n<div class=\"code-block\" data-language=\"default\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Code</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Code code\"><code><span class=\"kn\">from</span><span class=\"w\"> </span><span class=\"nn\">django.template</span><span class=\"w\"> </span><span class=\"kn\">import</span> <span class=\"n\">Context</span>\n<span class=\"kn\">from</span><span class=\"w\"> </span><span class=\"nn\">django.template.loader</span><span class=\"w\"> </span><span class=\"kn\">import</span> <span class=\"n\">get_template</span>\n\n<span class=\"n\">template</span> <span class=\"o\">=</span> <span class=\"n\">get_template</span><span class=\"p\">(</span><span class=\"s1\">&#39;hello.html&#39;</span><span class=\"p\">)</span>\n<span class=\"n\">html</span> <span class=\"o\">=</span> <span class=\"n\">template</span><span class=\"o\">.</span><span class=\"n\">render</span><span class=\"p\">(</span><span class=\"n\">Context</span><span class=\"p\">({</span><span class=\"s1\">&#39;name&#39;</span><span class=\"p\">:</span> <span class=\"s1\">&#39;world&#39;</span><span class=\"p\">}))</span>\n</code></pre></div>\n<p>You should write:</p>\n<div class=\"code-block\" data-language=\"default\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Code</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Code code\"><code><span class=\"kn\">from</span><span class=\"w\"> </span><span class=\"nn\">django.template.loader</span><span class=\"w\"> </span><span class=\"kn\">import</span> <span class=\"n\">get_template</span>\n\n<span class=\"n\">template</span> <span class=\"o\">=</span> <span class=\"n\">get_template</span><span class=\"p\">(</span><span class=\"s1\">&#39;hello.html&#39;</span><span class=\"p\">)</span>\n<span class=\"n\">html</span> <span class=\"o\">=</span> <span class=\"n\">template</span><span class=\"o\">.</span><span class=\"n\">render</span><span class=\"p\">({</span><span class=\"s1\">&#39;name&#39;</span><span class=\"p\">:</span> <span class=\"s1\">&#39;world&#39;</span><span class=\"p\">})</span>\n</code></pre></div>\n<p>And instead of:</p>\n<div class=\"code-block\" data-language=\"default\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Code</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Code code\"><code><span class=\"kn\">from</span><span class=\"w\"> </span><span class=\"nn\">django.template</span><span class=\"w\"> </span><span class=\"kn\">import</span> <span class=\"n\">RequestContext</span>\n<span class=\"kn\">from</span><span class=\"w\"> </span><span class=\"nn\">django.template.loader</span><span class=\"w\"> </span><span class=\"kn\">import</span> <span class=\"n\">get_template</span>\n\n<span class=\"n\">template</span> <span class=\"o\">=</span> <span class=\"n\">get_template</span><span class=\"p\">(</span><span class=\"s1\">&#39;hello.html&#39;</span><span class=\"p\">)</span>\n<span class=\"n\">html</span> <span class=\"o\">=</span> <span class=\"n\">template</span><span class=\"o\">.</span><span class=\"n\">render</span><span class=\"p\">(</span><span class=\"n\">RequestContext</span><span class=\"p\">(</span><span class=\"n\">request</span><span class=\"p\">,</span> <span class=\"p\">{</span><span class=\"s1\">&#39;name&#39;</span><span class=\"p\">:</span> <span class=\"s1\">&#39;world&#39;</span><span class=\"p\">}))</span>\n</code></pre></div>\n<p>You should write:</p>\n<div class=\"code-block\" data-language=\"default\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Code</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Code code\"><code><span class=\"kn\">from</span><span class=\"w\"> </span><span class=\"nn\">django.template.loader</span><span class=\"w\"> </span><span class=\"kn\">import</span> <span class=\"n\">get_template</span>\n\n<span class=\"n\">template</span> <span class=\"o\">=</span> <span class=\"n\">get_template</span><span class=\"p\">(</span><span class=\"s1\">&#39;hello.html&#39;</span><span class=\"p\">)</span>\n<span class=\"n\">html</span> <span class=\"o\">=</span> <span class=\"n\">template</span><span class=\"o\">.</span><span class=\"n\">render</span><span class=\"p\">({</span><span class=\"s1\">&#39;name&#39;</span><span class=\"p\">:</span> <span class=\"s1\">&#39;world&#39;</span><span class=\"p\">},</span> <span class=\"n\">request</span><span class=\"p\">)</span>\n</code></pre></div>\n<p>Passing a <a class=\"reference internal\" href=\"/en/1.9/ref/templates/api/#django.template.Context\" title=\"django.template.Context\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">Context</span></code></a> or a\n<a class=\"reference internal\" href=\"/en/1.9/ref/templates/api/#django.template.RequestContext\" title=\"django.template.RequestContext\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">RequestContext</span></code></a> is still possible when the template\nis loaded by a <a class=\"reference internal\" href=\"/en/1.9/topics/templates/#django.template.backends.django.DjangoTemplates\" title=\"django.template.backends.django.DjangoTemplates\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">DjangoTemplates</span></code></a>\nbackend but it’s deprecated and won’t be supported in Django 1.10.</p>\n<p>If you’re loading a template while you’re rendering another template with the\nDjango template language and you have access to the current context, for\ninstance in the <code class=\"docutils literal notranslate\"><span class=\"pre\">render()</span></code> method of a template tag, you can use the current\n<a class=\"reference internal\" href=\"/en/1.9/ref/templates/api/#django.template.Engine\" title=\"django.template.Engine\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">Engine</span></code></a> directly. Instead of:</p>\n<div class=\"code-block\" data-language=\"default\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Code</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Code code\"><code><span class=\"kn\">from</span><span class=\"w\"> </span><span class=\"nn\">django.template.loader</span><span class=\"w\"> </span><span class=\"kn\">import</span> <span class=\"n\">get_template</span>\n<span class=\"n\">template</span> <span class=\"o\">=</span> <span class=\"n\">get_template</span><span class=\"p\">(</span><span class=\"s1\">&#39;included.html&#39;</span><span class=\"p\">)</span>\n</code></pre></div>\n<p>You can write:</p>\n<div class=\"code-block\" data-language=\"default\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Code</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Code code\"><code><span class=\"n\">template</span> <span class=\"o\">=</span> <span class=\"n\">context</span><span class=\"o\">.</span><span class=\"n\">template</span><span class=\"o\">.</span><span class=\"n\">engine</span><span class=\"o\">.</span><span class=\"n\">get_template</span><span class=\"p\">(</span><span class=\"s1\">&#39;included.html&#39;</span><span class=\"p\">)</span>\n</code></pre></div>\n<p>This will load the template with the current engine without triggering the\nmultiple template engines machinery, which is usually the desired behavior.\nUnlike previous solutions, this returns a <a class=\"reference internal\" href=\"/en/1.9/ref/templates/api/#django.template.Template\" title=\"django.template.Template\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">django.template.Template</span></code></a>,\nlike <a class=\"reference internal\" href=\"/en/1.9/topics/templates/#django.template.loader.get_template\" title=\"django.template.loader.get_template\"><code class=\"xref py py-func docutils literal notranslate\"><span class=\"pre\">get_template()</span></code></a> used to in Django 1.7 and\nearlier, avoiding all backwards-compatibility problems.</p>\n</section>\n<section id=\"get-template-from-string\">\n<h3><code class=\"docutils literal notranslate\"><span class=\"pre\">get_template_from_string()</span></code><a class=\"heading-anchor\" href=\"#get-template-from-string\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>Private API <code class=\"docutils literal notranslate\"><span class=\"pre\">get_template_from_string(template_code)</span></code> was removed in Django\n1.8 because it had no way to choose an engine to compile the template.</p>\n<p>Three alternatives are available.</p>\n<p>If you control the project’s setting, you can use one of the configured\nengines:</p>\n<div class=\"code-block\" data-language=\"default\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Code</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Code code\"><code><span class=\"kn\">from</span><span class=\"w\"> </span><span class=\"nn\">django.template</span><span class=\"w\"> </span><span class=\"kn\">import</span> <span class=\"n\">engines</span>\n\n<span class=\"n\">template</span> <span class=\"o\">=</span> <span class=\"n\">engines</span><span class=\"p\">[</span><span class=\"s1\">&#39;django&#39;</span><span class=\"p\">]</span><span class=\"o\">.</span><span class=\"n\">from_string</span><span class=\"p\">(</span><span class=\"n\">template_code</span><span class=\"p\">)</span>\n</code></pre></div>\n<p>This returns a backend-dependent <code class=\"docutils literal notranslate\"><span class=\"pre\">Template</span></code> object.</p>\n<p>For trivial templates that don’t need context processors nor anything else,\nyou can create a bare-bones engine and use its <code class=\"docutils literal notranslate\"><span class=\"pre\">from_string()</span></code> method:</p>\n<div class=\"code-block\" data-language=\"default\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Code</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Code code\"><code><span class=\"kn\">from</span><span class=\"w\"> </span><span class=\"nn\">django.template</span><span class=\"w\"> </span><span class=\"kn\">import</span> <span class=\"n\">Engine</span>\n\n<span class=\"n\">template</span> <span class=\"o\">=</span> <span class=\"n\">Engine</span><span class=\"p\">()</span><span class=\"o\">.</span><span class=\"n\">from_string</span><span class=\"p\">(</span><span class=\"n\">template_code</span><span class=\"p\">)</span>\n</code></pre></div>\n<p>This returns a <a class=\"reference internal\" href=\"/en/1.9/ref/templates/api/#django.template.Template\" title=\"django.template.Template\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">django.template.Template</span></code></a> because\n<a class=\"reference internal\" href=\"/en/1.9/ref/templates/api/#django.template.Engine\" title=\"django.template.Engine\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">Engine</span></code></a> is part of the Django template language’s\nAPIs. The multiple template engines machinery isn’t involved here.</p>\n<p>Finally, if you have access to the current context, you can use the same trick\nas above:</p>\n<div class=\"code-block\" data-language=\"default\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Code</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Code code\"><code><span class=\"n\">template</span> <span class=\"o\">=</span> <span class=\"n\">context</span><span class=\"o\">.</span><span class=\"n\">template</span><span class=\"o\">.</span><span class=\"n\">engine</span><span class=\"o\">.</span><span class=\"n\">from_string</span><span class=\"p\">(</span><span class=\"n\">template_code</span><span class=\"p\">)</span>\n</code></pre></div>\n</section>\n</section>\n<section id=\"template\">\n<h2><code class=\"docutils literal notranslate\"><span class=\"pre\">Template()</span></code><a class=\"heading-anchor\" href=\"#template\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<p>To a lesser extent, instantiating a template with <code class=\"docutils literal notranslate\"><span class=\"pre\">Template(template_code)</span></code>\nsuffers from the same issue as <code class=\"docutils literal notranslate\"><span class=\"pre\">get_template_from_string()</span></code>.</p>\n<p>It still works when the <a class=\"reference internal\" href=\"/en/1.9/ref/settings/#std-setting-TEMPLATES\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">TEMPLATES</span></code></a> setting defines exactly one\n<a class=\"reference internal\" href=\"/en/1.9/topics/templates/#django.template.backends.django.DjangoTemplates\" title=\"django.template.backends.django.DjangoTemplates\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">DjangoTemplates</span></code></a> backend, but\npluggable applications can’t control this requirement.</p>\n<p>The last two solutions described in the previous section are recommended in\nthat case.</p>\n</section>","rootId":"upgrading-templates-to-django-1-8","toc":[{"title":"The TEMPLATES settings","anchor":"the-templates-settings","children":[]},{"title":"django.template.loader","anchor":"django-template-loader","children":[{"title":"get_template() and select_template()","anchor":"get-template-and-select-template","children":[]},{"title":"get_template_from_string()","anchor":"get-template-from-string","children":[]}]},{"title":"Template()","anchor":"template","children":[]}],"breadcrumbs":[{"docname":"ref/index","title":"API Reference","url":"/en/1.9/ref/"},{"docname":"ref/templates/index","title":"Templates","url":"/en/1.9/ref/templates/"}],"prev":{"docname":"ref/templates/api","title":"The Django template language: for Python programmers","url":"/en/1.9/ref/templates/api/"},"next":{"docname":"ref/template-response","title":"TemplateResponse and SimpleTemplateResponse","url":"/en/1.9/ref/template-response/"},"formats":{"html":"/en/1.9/ref/templates/upgrading/","markdown":"/en/1.9/ref/templates/upgrading.md","json":"/en/1.9/ref/templates/upgrading.json"},"source":"https://github.com/django/django/blob/stable/1.9.x/docs/ref/templates/upgrading.txt","official":"https://docs.djangoproject.com/en/1.9/ref/templates/upgrading/","inVersions":["1.11","1.10","1.9","1.8"],"inLocales":["en","fr","ja","id","pt-br","es"]}