Skip to main content

Django reusable app to simplify form construction

Project description

https://github.com/idlesign/django-siteforms

release lic ci coverage

Description

Django reusable app to simplify form construction

For those who consider maintaining templates-based forms solutions for Django a burden.

Features:

  • Full form rendering support, including prolog and submit button

  • Subforms support (represent entire other form as form field)

  • Field groups

  • Declarative attributes for elements

  • Simplified declarative forms layout, allowing fields ordering

  • Aria-friendly (Accessible Rich Internet Applications)

Supported styling:

  • No CSS

  • Bootstrap 4

Usage

To render a form in templates just address a variable, e.g. <div>{{ form }}</div>.

Basic

Let’s show how to build a simple form.

from django.shortcuts import render
from siteforms.composers.bootstrap4 import Bootstrap4
from siteforms.toolbox import ModelForm


class MyForm(ModelForm):
    """This form will show us how siteforms works."""

    disabled_fields = {'somefield'}  # One way of disabling fields.
    hidden_fields = {'otherfield'}  # One way of hiding fields.

    class Composer(Bootstrap4):
        """This will instruct siteforms to compose this
        form using Bootstrap 4 styling.

        """
    class Meta:
        model = MyModel  # Suppose you have a model class already.
        fields = '__all__'

def my_view(request):
    # Initialize form using data from POST.
    my_form = MyForm(request=request, src='POST')
    is_valid = form.is_valid()
    return render(request, 'mytemplate.html', {'form': my_form})

Composer options

Now let’s see how to tune our form.

from siteforms.composers.bootstrap4 import Bootstrap4, FORM, ALL_FIELDS

class Composer(Bootstrap4):

    opt_size='sm',  # Bootstrap 4 has sizes, so let's make our form small.

    # Element (fields, groups, form, etc.) attributes are ruled by `attrs`.
    # Let's add rows=2 to our `contents` model field.
    attrs={'contents': {'rows': 2}},

    # To group fields into named groups describe them in `groups`.
    groups={
        'basic': 'Basic attributes',
        'other': 'Other fields',
    },

    # We apply custom layout to our form.
    layout = {
        FORM: {
            'basic': [  # First we place `basic` group.
                ['title', 'date_created'],  # These two fields go into a row.
                'contents',  # This one field goes into a separate row.
            ],
            # We place all the rest fields into `other` group.
            'other': ALL_FIELDS,
        }
    }

Documentation

https://django-siteforms.readthedocs.org/

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

django-siteforms-0.2.0.tar.gz (20.0 kB view details)

Uploaded Source

Built Distribution

django_siteforms-0.2.0-py2.py3-none-any.whl (18.6 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file django-siteforms-0.2.0.tar.gz.

File metadata

  • Download URL: django-siteforms-0.2.0.tar.gz
  • Upload date:
  • Size: 20.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: Python-urllib/3.6

File hashes

Hashes for django-siteforms-0.2.0.tar.gz
Algorithm Hash digest
SHA256 7af5ed02fa92d280343823b513eb6a6e2874849b255b9eac2362434b49013b97
MD5 7629d02a442aad67c643810c3ed5fcf5
BLAKE2b-256 3d32b886a5a087f836ad75ec58786d5420e54de7be7c53f281548ccf9e9f8df2

See more details on using hashes here.

File details

Details for the file django_siteforms-0.2.0-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for django_siteforms-0.2.0-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 665e0527bd7fc7228f24ea6681511b0b7d6b12ecd99943c3ebe0b747613fbc88
MD5 f8c5dc05a5a49ff0f2bb1d5a251f399d
BLAKE2b-256 49e803b3e6763a0cc8aeabf0a6293a391ca00b8c8091cb5d7ca5f178aadbe539

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page