【问题标题】:How to use CSS with Django forms?如何在 Django 表单中使用 CSS?
【发布时间】:2014-10-17 03:53:37
【问题描述】:

我已经完成了HTMLCSS 模板,现在我必须使用PythonDjango 为系统赋予生命。但我陷入了困境:如何将我的cssdjango forms 一起使用?

例如,这是我在 Django 中使用的:

<label for="id_username">Username:</label>
{{ form.username }}

这就是 django 生成的:

<input id="id_username" maxlength="100" name="username" type="text" />

由于表单中的字段是 django 自动生成的,我如何为 input 字段和其他字段应用样式? 这是我的登录 form 例如,我想使用 django 和到目前为止完成的 css,但是如何?

<form class="form-signin" action="" method="post">
  <h2 class="form-signin-heading">Pydorado</h2>
    <div class="login-wrap">
      <div class="user-login-info">
        <input type="text" class="form-control" placeholder="User ID" autofocus>
        <input type="password" class="form-control" placeholder="Password">
      </div>
      //..
    </div>
</form>

我阅读了与此相关的django 文档,但到目前为止我找不到任何解决方案。有什么想法或建议吗?

【问题讨论】:

    标签: python html css django django-forms


    【解决方案1】:

    您可以通过添加如下额外属性在表单域中应用您的样式:

    myfield = forms.CharField(widget=forms.TextInput(attrs={'class':'form-control'}))
    

    我还看到了一个巧妙的解决方案,有人创建了a custom template tag,您可以在模板本身中添加类。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-06-15
      • 2011-07-23
      • 2012-04-29
      • 2020-02-10
      • 1970-01-01
      • 2019-12-06
      • 2021-08-08
      • 2019-12-17
      相关资源
      最近更新 更多