📜  django crispy formsfoundation for site - Python (1)

📅  最后修改于: 2023-12-03 15:00:26.323000             🧑  作者: Mango

Django Crispy Forms Foundation

Django Crispy Forms Foundation is a package that provides Django-Crispy-Forms integration with Foundation, a front-end framework for building responsive sites and web applications.

Features

Django Crispy Forms Foundation provides the following features:

  • A Foundation layout object for rendering forms with Foundation's grid system.
  • A FoundationAccordion layout object for rendering forms with Foundation's accordion.
  • A FoundationTabHolder layout object for rendering forms with Foundation's tabs.
  • A FoundationSwitch layout object for rendering forms with Foundation's switch.
  • A FoundationDatePicker layout object for rendering forms with Foundation's date picker.
Installation

To install Django Crispy Forms Foundation, run the following command:

pip install django-crispy-forms-foundation

After installation, add 'crispy_forms' and 'crispy_forms_foundation' to your INSTALLED_APPS setting.

INSTALLED_APPS = [
    # ...
    'crispy_forms',
    'crispy_forms_foundation',
    # ...
]
Usage
Foundation Layout

To use the Foundation layout object, set the form_class on your form to 'foundation'.

from django import forms
from crispy_forms.helper import FormHelper
from crispy_forms.layout import Layout, Fieldset
from crispy_forms_foundation.layout import Foundation

class ExampleForm(forms.Form):
    name = forms.CharField()
    email = forms.EmailField()

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.helper = FormHelper()
        self.helper.layout = Layout(
            Fieldset(
                'Information',
                'name',
                'email',
                layout_class=Foundation
            )
        )
Foundation Accordion

To use the FoundationAccordion layout object, set the layout_class on your fieldset to FoundationAccordion.

from django import forms
from crispy_forms.helper import FormHelper
from crispy_forms.layout import Layout, Fieldset
from crispy_forms_foundation.layout import FoundationAccordion

class ExampleForm(forms.Form):
    name = forms.CharField()
    email = forms.EmailField()

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.helper = FormHelper()
        self.helper.layout = Layout(
            Fieldset(
                'Information',
                'name',
                'email',
                layout_class=FoundationAccordion
            )
        )
Foundation Tab Holder

To use the FoundationTabHolder layout object, set the layout_class on your fieldset to FoundationTabHolder.

from django import forms
from crispy_forms.helper import FormHelper
from crispy_forms.layout import Layout, Fieldset
from crispy_forms_foundation.layout import FoundationTabHolder

class ExampleForm(forms.Form):
    name = forms.CharField()
    email = forms.EmailField()

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.helper = FormHelper()
        self.helper.layout = Layout(
            Fieldset(
                'Information',
                'name',
                'email',
                layout_class=FoundationTabHolder
            )
        )
Foundation Switch

To use the FoundationSwitch layout object, set the layout_class on your form fields to FoundationSwitch.

from django import forms
from crispy_forms.helper import FormHelper
from crispy_forms.layout import Layout, Fieldset
from crispy_forms_foundation.layout import Foundation

class ExampleForm(forms.Form):
    name = forms.CharField(widget=forms.TextInput(attrs={'data-toggle': 'toggle'}), label='Name', required=False)
    email = forms.EmailField(widget=forms.TextInput(attrs={'data-toggle': 'toggle'}), label='Email', required=False)

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.helper = FormHelper()
        self.helper.layout = Layout(
            Fieldset(
                'Information',
                'name',
                'email',
                layout_class=Foundation
            )
        )
Foundation Date Picker

To use the FoundationDatePicker layout object, set the layout_class on your form fields to FoundationDatePicker.

from django import forms
from crispy_forms.helper import FormHelper
from crispy_forms.layout import Layout, Fieldset
from crispy_forms_foundation.layout import Foundation

class ExampleForm(forms.Form):
    name = forms.CharField(label='Name', required=False)
    date = forms.DateField(
        label='Date',
        widget=forms.TextInput(attrs={'data-datepicker': 'true'}), 
        input_formats=['%m/%d/%Y'],
        required=False
    )

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.helper = FormHelper()
        self.helper.layout = Layout(
            Fieldset(
                'Information',
                'name',
                'date',
                layout_class=Foundation
            )
        )
Conclusion

Django Crispy Forms Foundation is a powerful package that provides seamless integration between Django-Crispy-Forms and Foundation Front-end Framework. With its various layout objects and widgets, you can create responsive forms with ease, saving you valuable time and effort. Try it out and explore its features to see how it can enhance your Django development experience.