python - 如何在 Django 表单中使用 CSS?

标签 python html css django django-forms

我已经完成了 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 字段和其他字段应用样式? 例如,这是我的登录表单,我想将 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与此相关的文档,但到目前为止我找不到任何解决方案。有什么想法或建议吗?

最佳答案

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

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

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

关于python - 如何在 Django 表单中使用 CSS?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25461492/

相关文章:

python - pip install myclient 错误 - 在 Windows 7 上使用 Python 3.7.0

python - 如何让Python中的所有线程休眠

python - 控制 Python 打印行数的变量

html - 使用具有 'style' 属性的 HTML 标记呈现 HTML 文本

html - 创建下拉子菜单

c++ - EXIF 之类的视频

java - html <img> 不显示

javascript - 如何正确绑定(bind)CoreUI Switch?

javascript - 从javascript中的另一个函数调用一个函数

html - CSS 使用绝对 div 模糊并保留锐边