I tried to generate help text for a Choice Field in my Django form with
我试着用我的Django表单为Choice Field生成帮助文本
i_agree = forms.CharField(label="", help_text="Initial to affirm that you agree to the <a href='/contract.pdf'>contract</a>.", required=True, max_length="4")
However, the raw HTML is rendered as output in the help text. How do I input HTML into the help text of a Django form field?
但是,原始HTML将在帮助文本中呈现为输出。如何在Django表单字段的帮助文本中输入HTML?
4 个解决方案
#1
37
You can use mark_safe
in the model to indicate the html is safe and it should be interpreted as such:
您可以在模型中使用mark_safe来指示html是安全的,它应该被解释为:
from django.utils.safestring import mark_safe
i_agree = forms.CharField(label="", help_text=mark_safe("Initial to affirm that you agree to the <a href='/contract.pdf'>contract</a>."), required=True, max_length="4")
#2
7
You can alternatively mark it as safe in the template if you loop through the form yourself:
如果您自己遍历表单,也可以在模板中将其标记为安全:
{% for f in form %}
{{f.label}}{{f}}{{f.help_text|safe}}
{%endfor%}
That's a very simple example of doing so in the template. You would need to do more than that to make it look nice.
这是在模板中这样做的一个非常简单的例子。你需要做的不仅仅是让它看起来不错。
#3
2
You can use an external file to increase maintainability and separation of concern:
您可以使用外部文件来提高可维护性和关注点分离:
- modify yourform's
__init__()
method ; - 修改yourform的__init __()方法;
- after the
super(MyForm, self).__init__(*args, **kwargs)
; - 超级之后(MyForm,self).__ init __(* args,** kwargs);
- assign the result of
render_to_string()
toself.fields['my_field'].help_text
. - 将render_to_string()的结果分配给self.fields ['my_field']。help_text。
forms.py
from django.template.loader import render_to_string
来自django.template.loader导入render_to_string
class MyForm(forms.ModelForm):
def __init__(self, *args, **kwargs):
super(MyForm, self).__init__(*args, **kwargs)
# Only in case we build the form from an instance,
if 'instance' in kwargs and kwargs['instance'].nature == consts.RiskNature.RPS:
self.fields['my_field'].help_text = render_to_string('components/my-template.html')
# ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^
my-template.html
{% load i18n %}
<table class="table table-bordered">
<tr>
<th>{% trans 'Externe' %}</th>
</tr>
<tbody class="text-muted">
<tr>
<td>{% trans "En lien avec les relations de travail" %}</td>
</tr>
<tr>
<td>{% trans "-" %}</td>
</tr>
</tbody>
</table>
#4
-6
Use a textarea tag with readonly
使用带readonly的textarea标签
<textarea readonly> <p>stuff</p> </textarea>
#1
37
You can use mark_safe
in the model to indicate the html is safe and it should be interpreted as such:
您可以在模型中使用mark_safe来指示html是安全的,它应该被解释为:
from django.utils.safestring import mark_safe
i_agree = forms.CharField(label="", help_text=mark_safe("Initial to affirm that you agree to the <a href='/contract.pdf'>contract</a>."), required=True, max_length="4")
#2
7
You can alternatively mark it as safe in the template if you loop through the form yourself:
如果您自己遍历表单,也可以在模板中将其标记为安全:
{% for f in form %}
{{f.label}}{{f}}{{f.help_text|safe}}
{%endfor%}
That's a very simple example of doing so in the template. You would need to do more than that to make it look nice.
这是在模板中这样做的一个非常简单的例子。你需要做的不仅仅是让它看起来不错。
#3
2
You can use an external file to increase maintainability and separation of concern:
您可以使用外部文件来提高可维护性和关注点分离:
- modify yourform's
__init__()
method ; - 修改yourform的__init __()方法;
- after the
super(MyForm, self).__init__(*args, **kwargs)
; - 超级之后(MyForm,self).__ init __(* args,** kwargs);
- assign the result of
render_to_string()
toself.fields['my_field'].help_text
. - 将render_to_string()的结果分配给self.fields ['my_field']。help_text。
forms.py
from django.template.loader import render_to_string
来自django.template.loader导入render_to_string
class MyForm(forms.ModelForm):
def __init__(self, *args, **kwargs):
super(MyForm, self).__init__(*args, **kwargs)
# Only in case we build the form from an instance,
if 'instance' in kwargs and kwargs['instance'].nature == consts.RiskNature.RPS:
self.fields['my_field'].help_text = render_to_string('components/my-template.html')
# ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^
my-template.html
{% load i18n %}
<table class="table table-bordered">
<tr>
<th>{% trans 'Externe' %}</th>
</tr>
<tbody class="text-muted">
<tr>
<td>{% trans "En lien avec les relations de travail" %}</td>
</tr>
<tr>
<td>{% trans "-" %}</td>
</tr>
</tbody>
</table>
#4
-6
Use a textarea tag with readonly
使用带readonly的textarea标签
<textarea readonly> <p>stuff</p> </textarea>