猿问

如何将引导程序设计放在 django 表单上的 CheckboxSelectMultiple 中

我在数据库中有一个动态列需求,在我的 forms.py 中,我有这个 RequirementForm 来将数据库中的需求行数填充到 html。


class RequirementForm(forms.ModelForm):


    class Meta:

        model = ApplicantInfo


        fields = ('requirement',)

        widgets = {

            'requirement': forms.CheckboxSelectMultiple,

        }

有没有办法在这个上放置引导程序设计?因为在我的 html 代码中,我只有这个:


{% extends 'applicant/base.html' %}


{% block content %}

<center>

    <!-- Default unchecked -->

    <form method = "post">

        {% csrf_token %}

        {{ form }}

        <button type="submit">Save</button>

    </form>

</center>

{% endblock %}


肥皂起泡泡
浏览 183回答 2
2回答

料青山看我应如是

在表单中添加一个 init fun:&nbsp; &nbsp; def __init__(self, *args, **kwargs):&nbsp; &nbsp; &nbsp; &nbsp; super().__init__(*args, **kwargs)&nbsp; &nbsp; &nbsp; &nbsp; for myField in self.fields:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; self.fields[myField].widget.attrs['class'] = 'form-control'另外,记得在模板中添加 bootstrap CSS 和 js

慕码人8056858

您可以使用 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>&nbsp; &nbsp; <!-- Default unchecked -->&nbsp; &nbsp; <form method = "post">&nbsp; &nbsp; &nbsp; &nbsp; {% csrf_token %}&nbsp; &nbsp; &nbsp; &nbsp; {{ form|crispy }}&nbsp; &nbsp; &nbsp; &nbsp; <button type="submit" class="btn btn-default">Save</button>&nbsp; &nbsp; </form></center>{% endblock %}
随时随地看视频慕课网APP

相关分类

Python
我要回答