How to inject Django template code into template through {{ variable/method }}?
<p>I have a model Reservation which I use in many templates. It's handy to create it's own <code>HTML/Django</code> snippet which is being injected into the template through variable/model method. </p>

<p>The raw <code>HTML</code> is correct using the method but Django template language isn't interpreted correctly. </p>

<p>This is a Reservation method:</p>

<pre><code>def get_html_description(self):
    return """&lt;ul&gt;
                &lt;li&gt;&lt;b&gt;ID:&lt;/b&gt; {{ reservation.id }}&lt;/li&gt;
                &lt;hr&gt;
                &lt;li&gt;&lt;b&gt;From:&lt;/b&gt; {{ reservation.get_text_destination_from }}&lt;/li&gt;
                &lt;li&gt;&lt;b&gt;To:&lt;/b&gt; {{ reservation.get_text_destination_to }}&lt;/li&gt;
                &lt;hr&gt;
                &lt;li&gt;&lt;b&gt;Date:&lt;/b&gt; {{ reservation.get_date }}&lt;/li&gt;
                &lt;li&gt;&lt;b&gt;Time:&lt;/b&gt; {{ reservation.get_time }}&lt;/li&gt;
              &lt;/ul&gt;"""
</code></pre>

<p>Now I'm trying to inject this code into the template:</p>

<pre><code>&lt;div class="events"&gt;
     {% for reservation in data.1 %}
         &lt;div class="event"&gt;
              &lt;h4&gt;{{ reservation.get_text_destination_from }} to {{ reservation.get_text_destination_to }}&lt;/h4&gt;
              &lt;div class="desc"&gt;
                  {% autoescape off %}{{ reservation.get_html_description }}{% endautoescape %}
              &lt;/div&gt;...
                    ...
</code></pre>

<p>Unfortunately it renders something like this:</p>

<p><a href="http://i.stack.imgur.com/hP2a1.png" rel="nofollow"><img src="http://i.stack.imgur.com/hP2a1.png" alt="enter image description here"></a></p>

<p>Do you know what to do? I've already tried filter <code>|safe</code> and <code>{% autoescape off %}</code></p>

<p>Simply don't ask the template to do it, if you really want to continue to use this method then just do string formatting, and mark it as safe.</p>

<pre><code>desc = """&lt;ul&gt;
            &lt;li&gt;&lt;b&gt;ID:&lt;/b&gt; %(id)s&lt;/li&gt;
          &lt;/ul&gt;""" % { 'id': self.id }

return mark_safe(desc)
</code></pre>

<p>Etc.</p>

python html django templates django-templates