我创建了一个django表单并将其输出到模板中。但是我不明白如何添加引导程序样式,因为这种django形式不好。
输出图片:
代码:
{% 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 %}
答案 0 :(得分:1)
要将引导样式添加到表单中,您需要将此代码粘贴到CSS标签之间的某个位置;
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" crossorigin="anonymous">
这是针对JavaScript的;
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js" integrity="sha384-ZMP7rVo3mIykV+2+9J3UJ46jBk0WLaUAdn689aCwoqbBJiSnjAK/l8WvCWPIPm49" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js" integrity="sha384-ChfqqxuZUCnJSK3+MXmPNIyE6ZbWh2IMqE241rYiqJxyMiZ6OW/JmZQ5stwEULTy" crossorigin="anonymous"></script>
这将对您的表单应用一些样式。
完成此操作后,如果要更改样式,则必须使用在BootStrap文档中指定的类,您可以在此处找到这些类:https://getbootstrap.com/docs/4.1/components/forms/
这应该可以为您提供所需的样式,尽管您可能希望另外添加一些自己的样式,以得到您想要的东西。
答案 1 :(得分:0)
将此添加到您的表单:
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
for myField in self.fields:
self.fields[myField].widget.attrs['class'] = 'form-control'