python - 如何将 Bootstrap 样式添加到我的 Django 表单中?

标签 python html css django twitter-bootstrap

我创建了一个 Django 表单并将其输出到模板中。但我不明白如何添加 Bootstrap 样式,因为这种 django 形式不好。

输出图像:

enter image description here

代码:

{% extends 'base.html' %}
{% block title %}
    Contact Us
{% endblock %}

{% block content %}
<h1>Contact Us</h1>
<form method="post">
        {% csrf_token %}

        {% for field in form %}
            <div class="form-group">
                {{ field.label }}
                {{ field }}
            </div>
        {% endfor %}
        <small id="emailHelp" class="form-text text-muted">We'll never share your email with anyone else.</small>
        <button class="btn btn-primary" type="submit" name="button">Send</button>
    </form>
{% endblock %}

最佳答案

将此添加到您的表单中:

def __init__(self, *args, **kwargs):
    super().__init__(*args, **kwargs)
    for myField in self.fields:
        self.fields[myField].widget.attrs['class'] = 'form-control'

关于python - 如何将 Bootstrap 样式添加到我的 Django 表单中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54114635/

相关文章:

python - Pandas reset_index() 在通过多种方法分组和聚合后不起作用

python - numpy 数组中的额外小数?

python - python 的 matlab 引擎中的 save() 命令

python - 如何在我的 github 自述文件中设置 pyversions 图标?

javascript - 更改动态添加元素的属性

php - 生产和 XAMPP 上的不同 css

php - 限制桌面上网页的大小调整和缩放

Javascript:导入错误并且找不到变量

php - 尝试将 JSON 加载到 HTML 音频标签时出现解析错误

javascript - CSS保持背景视频居中