django crispy 形式的内联形式
inline form with django crispy form
抱歉,我就是不明白,这里的 docs 非常棒,我使用的是几乎相同的示例,我只有两个字段,我想显示内联,但它不起作用,
我的表格:
from django import forms
from crispy_forms.helper import FormHelper
from crispy_forms import layout, bootstrap
from crispy_forms.bootstrap import InlineField, FormActions, StrictButton
from crispy_forms.layout import Layout
from ..models import EmployeeModel
class EmployeeCreateForm(forms.ModelForm):
"""
TODO: Extend CompanyModel into Form
:returns: TODO
"""
def __init__(self, *args, **kwargs):
super(EmployeeCreateForm, self).__init__(*args, **kwargs)
self.helper = FormHelper()
self.helper.form_tag = False
self.helper.method = "POST"
self.helper.form_class = 'form-inline'
self.helper.field_template = 'bootstrap3/layout/inline_field.html'
self.helper.form_action = "company:create-employee"
self.helper.layout = Layout(
'first_name',
'last_name',
StrictButton('Add', css_class='btn-default'),
)
class Meta:
model = EmployeeModel
fields = ["first_name", "last_name"]
和我的模板:
{% extends "base.html" %}
{% load i18n static %}
{% load crispy_forms_tags %}
{% block content %}
<nav class="navbar fixed-top navbar-light bg-faded">
<ul class="nav justify-content-center">
<li class="nav-item">
<a class="nav-link" href="{% url 'why' %}">WHY SCREEN?</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">BLOG</a>
</li>
<li class="nav-item">
<a class="nav-link" href="{% url 'faq' %}">FAQ</a>
</li>
<li class="nav-item">
<a class="nav-link" href="{% url 'about' %}">ABOUT</a>
</li>
</ul>
</nav>
<div class="container">
<div class="row">
<p style="padding:60px;"></p>
</div>
</div>
<div class="container">
<div class="row">
<div class="col-sm-4 col-sm-offset-2">
<form action="{% url 'company:create-employee' %}" method="post">
{% csrf_token %}
{% crispy form %}
</form>
</div>
</div>
</div>
但是表单仍然是水平呈现的,它不是内联的,所以我在这里遗漏了什么,请有人解释一下。
终于搞定了,是这样的
from django import forms
from django.forms.models import formset_factory
from crispy_forms.helper import FormHelper
from crispy_forms import layout, bootstrap
from crispy_forms.bootstrap import InlineField, FormActions, StrictButton, Div
from crispy_forms.layout import Layout
from crispy_forms import bootstrap, layout
from company.models.employee_model import EmployeeModel
class EmployeeCreateForm(forms.ModelForm):
"""
TODO: Extend CompanyModel into Form
:returns: TODO
"""
def __init__(self, *args, **kwargs):
super(EmployeeCreateForm, self).__init__(*args, **kwargs)
self.helper = FormHelper()
self.helper.form_tag = False
self.helper.method = "POST"
self.helper.form_class = 'form-inline'
self.helper.field_template = 'bootstrap3/layout/inline_field.html'
self.helper.form_action = "company:create-employee"
self.helper.layout = Layout(
Div(
Div('first_name', css_class="col-sm-2"),
Div('last_name', css_class="col-sm-2"),
Div('middle_initial', css_class="col-sm-2"),
Div('social_security_number', css_class="col-sm-2"),
bootstrap.FormActions(
layout.Submit('submit', 'Add', css_class='btn btn-primary')),
css_class='row',
)
)
class Meta:
model = EmployeeModel
fields = ["first_name", "last_name", "middle_initial", "social_security_number"]
我使用了 Div
和 css_class='row'
,我还添加了 css_class
到 Div's
,并且效果很好。表单现在内联显示。
Crispy Forms layouts 还可以让您通过以下方式执行此操作:
InlineCheckboxes('field_name')
抱歉,我就是不明白,这里的 docs 非常棒,我使用的是几乎相同的示例,我只有两个字段,我想显示内联,但它不起作用,
我的表格:
from django import forms
from crispy_forms.helper import FormHelper
from crispy_forms import layout, bootstrap
from crispy_forms.bootstrap import InlineField, FormActions, StrictButton
from crispy_forms.layout import Layout
from ..models import EmployeeModel
class EmployeeCreateForm(forms.ModelForm):
"""
TODO: Extend CompanyModel into Form
:returns: TODO
"""
def __init__(self, *args, **kwargs):
super(EmployeeCreateForm, self).__init__(*args, **kwargs)
self.helper = FormHelper()
self.helper.form_tag = False
self.helper.method = "POST"
self.helper.form_class = 'form-inline'
self.helper.field_template = 'bootstrap3/layout/inline_field.html'
self.helper.form_action = "company:create-employee"
self.helper.layout = Layout(
'first_name',
'last_name',
StrictButton('Add', css_class='btn-default'),
)
class Meta:
model = EmployeeModel
fields = ["first_name", "last_name"]
和我的模板:
{% extends "base.html" %}
{% load i18n static %}
{% load crispy_forms_tags %}
{% block content %}
<nav class="navbar fixed-top navbar-light bg-faded">
<ul class="nav justify-content-center">
<li class="nav-item">
<a class="nav-link" href="{% url 'why' %}">WHY SCREEN?</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">BLOG</a>
</li>
<li class="nav-item">
<a class="nav-link" href="{% url 'faq' %}">FAQ</a>
</li>
<li class="nav-item">
<a class="nav-link" href="{% url 'about' %}">ABOUT</a>
</li>
</ul>
</nav>
<div class="container">
<div class="row">
<p style="padding:60px;"></p>
</div>
</div>
<div class="container">
<div class="row">
<div class="col-sm-4 col-sm-offset-2">
<form action="{% url 'company:create-employee' %}" method="post">
{% csrf_token %}
{% crispy form %}
</form>
</div>
</div>
</div>
但是表单仍然是水平呈现的,它不是内联的,所以我在这里遗漏了什么,请有人解释一下。
终于搞定了,是这样的
from django import forms
from django.forms.models import formset_factory
from crispy_forms.helper import FormHelper
from crispy_forms import layout, bootstrap
from crispy_forms.bootstrap import InlineField, FormActions, StrictButton, Div
from crispy_forms.layout import Layout
from crispy_forms import bootstrap, layout
from company.models.employee_model import EmployeeModel
class EmployeeCreateForm(forms.ModelForm):
"""
TODO: Extend CompanyModel into Form
:returns: TODO
"""
def __init__(self, *args, **kwargs):
super(EmployeeCreateForm, self).__init__(*args, **kwargs)
self.helper = FormHelper()
self.helper.form_tag = False
self.helper.method = "POST"
self.helper.form_class = 'form-inline'
self.helper.field_template = 'bootstrap3/layout/inline_field.html'
self.helper.form_action = "company:create-employee"
self.helper.layout = Layout(
Div(
Div('first_name', css_class="col-sm-2"),
Div('last_name', css_class="col-sm-2"),
Div('middle_initial', css_class="col-sm-2"),
Div('social_security_number', css_class="col-sm-2"),
bootstrap.FormActions(
layout.Submit('submit', 'Add', css_class='btn btn-primary')),
css_class='row',
)
)
class Meta:
model = EmployeeModel
fields = ["first_name", "last_name", "middle_initial", "social_security_number"]
我使用了 Div
和 css_class='row'
,我还添加了 css_class
到 Div's
,并且效果很好。表单现在内联显示。
Crispy Forms layouts 还可以让您通过以下方式执行此操作:
InlineCheckboxes('field_name')