【问题标题】:Add a glyphicon even if i use Django即使我使用 Django,也要添加一个字形图标
【发布时间】:2018-05-04 22:00:38
【问题描述】:

您好,我想知道如何在代码中的“输入”左侧添加一个字形图标,因为我使用的是 django,而且我必须使用 forms.py。

我的 HTML 代码:

<form methode="post" action="/connexion" class="form-horizontal" role="form">
    {% csrf_token %}
    {{ form.as_p }}

    <div>
        <button type="submit" class="btn btn-info"><span class="glyphicon glyphicon-off">Connexion</button>
    </div>

</form>

forms.py:

class ConnexionForm(forms.Form):

user = forms.CharField(label='', max_length=30, widget=forms.TextInput(attrs={'placeholder': "Nom d'utilisateur"}))
password = forms.CharField(label='', widget=forms.PasswordInput)

【问题讨论】:

  • 使用crispy forms
  • @brunodesthuilliers 谢谢你,但我不明白如何添加字形图标
  • 如果您只需要添加图标,请单独渲染每个表单字段并在表单前添加一个 html 元素。喜欢&lt;i class="glyph..."&gt;&lt;/i&gt; {{form.name}} ...
  • @Beno 如果您知道如何将字形图标添加到手动构建的 html 表单中,那么您使用 django 表单所缺少的只是如何处理表单的渲染以获得所需的html,这就是本文所解释的内容(作为官方文档的补充)。

标签: python html django django-forms glyphicons


【解决方案1】:

您可以只手动渲染字段而不是渲染整个表单,例如:

<form ...>
    ...
    <i class="glyphicon..."></> {{ form.field_name }}
    ...
</form>

请参阅docs

【讨论】:

  • 嗯..有趣!我认为它解决了我的问题!我会试试的!太棒了
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-11-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-08-21
相关资源
最近更新 更多