内联形式与django脆皮形式

时间:2017-10-23 14:58:06

标签: django forms django-crispy-forms

我很抱歉,但是我还没有得到它,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>

但是表格仍然是横向呈现的,而且它不是内联的,所以我在这里失踪的内容可以让别人解释一下。

2 个答案:

答案 0 :(得分:1)

最后,我得到了它,就像这样

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"]

我已经使用了Divcss_class='row',而且我已将css_class添加到Div's并且效果很好。表单现在以内嵌方式显示。

答案 1 :(得分:0)

脆脆的表格layouts还可让您通过以下方式进行操作:

InlineCheckboxes('field_name')