2 回答

TA貢獻1772條經驗 獲得超8個贊
在表單中添加一個 init fun:
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
for myField in self.fields:
self.fields[myField].widget.attrs['class'] = 'form-control'
另外,記得在模板中添加 bootstrap CSS 和 js

TA貢獻1803條經驗 獲得超6個贊
您可以使用 django-crispy-forms https://django-crispy-forms.readthedocs.io/en/latest/
在你的 settings.py 中
CRISPY_TEMPLATE_PACK = 'bootstrap3' #or bootstrap4
有了它,您的模板將如下所示
{% extends 'applicant/base.html' %}
{% load crispy_forms_tags %}
{% block content %}
<center>
<!-- Default unchecked -->
<form method = "post">
{% csrf_token %}
{{ form|crispy }}
<button type="submit" class="btn btn-default">Save</button>
</form>
</center>
{% endblock %}
添加回答
舉報