Custom rendering of foreign field in form in django template
<p>In my modelform I have a foreign key, I cannot figure out how to change the appearance of this field in template. I can change the text by changing</p>

<pre><code>__unicode__ 
</code></pre>

<p>of the model, but how would I make it bold, for example?</p>

<p>in <strong>models.py</strong> I tried the following but form renders with  and all other tags as if they were just text:</p>

<pre><code>def __unicode__(self):
    u'&lt;b&gt;Name&lt;/b&gt;: {}\n&lt;b&gt;Loyal&lt;/b&gt;: {}'.format(self.name, self.loyal)
</code></pre>

<p>my <strong>template.html</strong>:</p>

<pre><code>  &lt;form method="post"&gt;
    {% csrf_token %}
    {{ form.client|safe}} 
    &lt;br&gt;
    &lt;input type="submit" value="Save changes" class="btn btn-s btn-success"&gt;
  &lt;/form&gt;
</code></pre>

<p>doesn't work.</p>

<p>Here is the picture:</p>

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

<p>Django 1.9 has a <code>format_html</code> function that might be what you are looking for. From <a href="https://docs.djangoproject.com/en/1.9/ref/utils/#django.utils.html.format_html" rel="nofollow">the Docs</a>:</p>

<blockquote>
  <p><code>format_html(format_string, *args, **kwargs)</code></p>
  
  <p>This is similar to str.format(), except that <strong>it is appropriate for building up HTML fragments</strong>. All args and kwargs are passed through conditional_escape() before being passed to str.format().</p>
  
  <p><strong>For the case of building up small HTML fragments, this function is to be preferred over string interpolation</strong> using % or str.format() directly, because it applies escaping to all arguments - just like the template system applies escaping by default.</p>
</blockquote>

<p>More information here: <a href="http://stackoverflow.com/questions/3298083/prevent-django-admin-from-escaping-html">Prevent django admin from escaping html</a></p>

python django django-forms