[英]Custom rendering of foreign field in form in django template
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
我可以通过更改文字来更改
__unicode__
of the model, but how would I make it bold, for example? 型号,但例如,如何将其设为大胆?
in models.py I tried the following but form renders with and all other tags as if they were just text: 在models.py中,我尝试了以下操作,但是使用和其他所有标签进行表单渲染,就好像它们只是文本一样:
def __unicode__(self):
u'<b>Name</b>: {}\n<b>Loyal</b>: {}'.format(self.name, self.loyal)
my template.html : 我的template.html :
<form method="post">
{% csrf_token %}
{{ form.client|safe}}
<br>
<input type="submit" value="Save changes" class="btn btn-s btn-success">
</form>
doesn't work. 不起作用。
Here is the picture: 这是图片:
Django 1.9 has a format_html
function that might be what you are looking for. Django 1.9具有您可能需要的
format_html
函数。 From the Docs : 从文档中 :
format_html(format_string, *args, **kwargs)
This is similar to str.format(), except that it is appropriate for building up HTML fragments .
这类似于str.format(),除了它适合于构建HTML片段 。 All args and kwargs are passed through conditional_escape() before being passed to str.format().
所有的args和kwargs在传递给str.format()之前都要先通过conditional_escape()传递。
For the case of building up small HTML fragments, this function is to be preferred over string interpolation using % or str.format() directly, because it applies escaping to all arguments - just like the template system applies escaping by default.
对于构建较小的HTML片段的情况,此函数比直接使用%或str.format() 进行字符串插补更可取,因为它对所有参数都应用了转义-就像模板系统默认情况下应用了转义一样。
More information here: Prevent django admin from escaping html 此处的更多信息: 防止django管理员转义html
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.