Skip to main content

Django reusable app to simplify form construction

Project description

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

release lic 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 a form field): JSON, Foreign Key, Many-to-Many

  • Field groups

  • Declarative attributes for elements

  • Simplified declarative forms layout, allowing fields ordering

  • Simple ways to make fields hidden, disabled, readonly

  • Support for fields from model’s properties

  • Aria-friendly (Accessible Rich Internet Applications)

  • Complex widgets (e.g. using values from multiple fields) support

  • Filter-forms (use form for queryset filtering)

Supported styling:

  • No CSS

  • Bootstrap 4

  • Bootstrap 5

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.bootstrap5 import Bootstrap5
from siteforms.toolbox import ModelForm


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

    disabled_fields = {'somefield'}  # Declarative way of disabling fields.
    hidden_fields = {'otherfield'}  # Declarative way of hiding fields.
    readonly_fields = {'anotherfield'}  # Declarative way of making fields readonly.

    class Composer(Bootstrap5):
        """This will instruct siteforms to compose this
        form using Bootstrap 5 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.bootstrap5 import Bootstrap5, FORM, ALL_FIELDS

class Composer(Bootstrap5):

    opt_size='sm'  # Bootstrap 5 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.
                # The following three fields are in the same row -
                # two fields in the right column are stacked.
                ['title', ['date_created',
                           'date_updated']],
                '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-1.1.0.tar.gz (39.7 kB view details)

Uploaded Source

Built Distribution

django_siteforms-1.1.0-py3-none-any.whl (41.8 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: django-siteforms-1.1.0.tar.gz
  • Upload date:
  • Size: 39.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 colorama/0.4.4 importlib-metadata/4.6.4 keyring/23.5.0 pkginfo/1.8.2 readme-renderer/34.0 requests-toolbelt/0.9.1 requests/2.25.1 rfc3986/1.5.0 tqdm/4.57.0 urllib3/1.26.5 CPython/3.10.6

File hashes

Hashes for django-siteforms-1.1.0.tar.gz
Algorithm Hash digest
SHA256 70123204cda3e9482391b6d914a2aaf49d2b4ffd6adc0b72ebdccb612f9bc9dc
MD5 c18bc5f65c6fdde9688516decb98d2a8
BLAKE2b-256 f751b52572c40f62edbaaa6c4ec37e2bb88a02331e8de59c06a701077d163971

See more details on using hashes here.

File details

Details for the file django_siteforms-1.1.0-py3-none-any.whl.

File metadata

  • Download URL: django_siteforms-1.1.0-py3-none-any.whl
  • Upload date:
  • Size: 41.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 colorama/0.4.4 importlib-metadata/4.6.4 keyring/23.5.0 pkginfo/1.8.2 readme-renderer/34.0 requests-toolbelt/0.9.1 requests/2.25.1 rfc3986/1.5.0 tqdm/4.57.0 urllib3/1.26.5 CPython/3.10.6

File hashes

Hashes for django_siteforms-1.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 5599dae043f57f3dcc910773915c74c452d520468797e0cc7af4d382ad6f4e2f
MD5 82c5d45219ba977ba26ac645f391202e
BLAKE2b-256 f2fac56d6050c8c6bb299a1f779ec860dbd1568304db3a0eb0b8cd8e35e22a3c

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