Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何将HTML输入到Django表单字段的帮助文本中?_Django_Django Forms - Fatal编程技术网

如何将HTML输入到Django表单字段的帮助文本中?

如何将HTML输入到Django表单字段的帮助文本中?,django,django-forms,Django,Django Forms,我尝试在Django表单中使用 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") i_agree=forms.CharField(label=“”,help_text=“初始确认您同意。”,required=

我尝试在Django表单中使用

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")
i_agree=forms.CharField(label=“”,help_text=“初始确认您同意。”,required=True,max_length=“4”)
但是,原始HTML在帮助文本中呈现为输出。如何将HTML输入到Django表单字段的帮助文本中?

您可以在模型中使用它来指示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")
来自django.utils.safestring导入标记\u safe
i_agree=forms.CharField(label=”“,help_text=mark_safe(“确认您同意的首字母”),required=True,max_length=“4”)

如果您自己循环表单,也可以在模板中将其标记为安全:

{% for f in form %}
    {{f.label}}{{f}}{{f.help_text|safe}}    
{%endfor%}

这是在模板中执行此操作的一个非常简单的示例。您需要做更多的事情才能使它看起来更漂亮。

使用带有readonly的textarea标记

<textarea readonly> <p>stuff</p> </textarea>
东西


您可以使用外部文件来提高可维护性和关注点分离:

  • 修改表单的
    \uuuu init\uuuu()方法
    
  • super(MyForm,self)之后
    
  • render\u to_string()
    的结果分配给
    self.fields['my\u field']。帮助\u text
  • forms.py 从django.template.loader导入渲染到字符串

    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
    {%loadi18n%}
    {%trans'外部“%”
    {%trans“En lien avec les relations de travail”}
    {%trans“-”%}
    
    这是一个很好的解决方案,但是它不能与
    表单的自动输出一起工作。as.[p,ul,table]
    。Swings and Roundounds-是的,将HTML放在外部文件中是很好的,但是重写的保存方法最终可能会充满不相关的功能-并且您已经将帮助文本设置在您不希望看到的地方。您可以将render\u to\u字符串直接放在字段定义本身上吗?您也可以将其直接应用到该字段的
    帮助文本
    self.fields['i\u agree'].help\u text=mark\u safe(“缩写以确认您同意。”)
    {% 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>