Skip to main content

Capybara forms

Dynamic Django forms and filters using JSON schema.

If you have 2 models: Category and Entry. And some entry features depends on it's category. Using this library, you will obtain easy-to-configure schema with form, filter and entry fields generators. Also, it can work with model fields.

Quick start

1. Define your models

from django.db import models
from capybara_forms.models import CapybaraFormsCategory, CapybaraFormsModel


class Category(CapybaraFormsCategory):
    title = models.CharField(max_length=100)

class Advert(CapybaraFormsModel(Category)):
    # Global model fields
    title = models.CharField(max_length=100)
    price = models.PositiveSmallIntegerField()
    # Rest fields will be stored in JSON 'data' field

2. Write schema for category 'Cats'

[
  {
    "name": "breed",
    "type": "string",
    "required": true,
    "display_name": "Breed",
    "placeholder": "Enter breed of your cat"
  },
  {
    "name": "year",
    "type": "number",
    "required": true,
    "max_value": 2020,
    "min_value": 1980,
    "placeholder": "Enter birth year of your cat",
    "display_name": "Birth year"
  },
  {
    "name": "weight",
    "type": "number",
    "placeholder": "Enter the weight of your cat",
    "display_name": "Weight"
  },
  {
    "name": "height",
    "type": "number",
    "placeholder": "Height (only placeholder, without display name)"
  },
  {
    "type": "color",
    "display_name": "Cat's color"
  },
  {
    "name": "name",
    "type": "string",
    "placeholder": "Enter cat nickname",
    "display_name": "Name"
  },
  {
    "name": "vaccinated",
    "type": "checkbox",
    "display_name": "is vaccinated"
  }
]

3. Create form. It's a basic rendering object

from django import forms
from capybara_forms.forms import CapybaraFormsModelForm
from .models import Advert


class AdvertForm(CapybaraFormsModelForm):
    # This fields from model will be rendered using form.render_form
    fields_in_model = ['title', 'price']

    # For custom placeholder
    title = forms.CharField(widget=forms.TextInput(attrs={'placeholder': 'Enter title'}))

    class Meta:
        model = Advert
        fields = ('title', 'price')

4. Use this form in your view

from django.shortcuts import render, get_object_or_404, redirect

from .models import Category
from .forms import AdvertForm


def add(request, category_id):
    category = get_object_or_404(Category, pk=category_id)
    if request.method == 'POST':
        form = AdvertForm(category, request.POST.dict())
        if form.is_valid():
            form.save()
            return redirect('/{0}/'.format(category_id))
    else:
        form = AdvertForm(category)

    return render(request, 'add.html', {
        'form': form,
    })

5. Render form in your template. Note, that library uses parsley for validation. All dependencies listed below

<form method="POST" data-parsley-validate>
    {% csrf_token %}
    {{ form.render_form | safe }}

    <button type="submit">Submit</button>
</form>

Result form:
Form image

Result filter, with similar schema, plus some in-form configuration:
Filter image

Dependencies

  1. Django
  2. jQuery (version 3.3.1 included)
  3. JSON editor (version 5.9.6 included)
  4. parsley (version 2.8.1 included)

Documentation and full example

See readthedocs for API, advanced usage cases, parameters definition and more information

See example project for all features, used in worked application

Slogan

With goal to simplify, with love to capybaras

Release files for capybara-forms 0.0.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for capybara-forms 0.0.2
File Size Uploaded
capybara_forms-0.0.2.tar.gz 234.4 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for capybara-forms 0.0.2
File Interpreter ABI Platform
capybara_forms-0.0.2-py3-none-any.whl Python 3 none any Details

Total release size: 478.7 kB

Release files / capybara_forms-0.0.2.tar.gz

Download URL capybara_forms-0.0.2.tar.gz
Size 234.4 kB
Tags Source
SHA-256 checksum
How to use checksums
fbde67ae0ec667df3da5c61d48a8a5014584d49fa5ce2db0e0282071e495837e
BLAKE2b-256 checksum
How to use checksums
7305aeb406575fdc31a7f1e4db504760c8d0e5d6b0aa080555e08dcc2658dee4
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.12.1 pkginfo/1.4.2 requests/2.21.0 setuptools/40.6.3 requests-toolbelt/0.8.0 tqdm/4.28.1 CPython/3.6.7

Release files / capybara_forms-0.0.2-py3-none-any.whl

Download URL capybara_forms-0.0.2-py3-none-any.whl
Size 244.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
4f5610197811910c8647b46acd938ee3ceb54107fbb6a7bcf5cd2239aaedd470
BLAKE2b-256 checksum
How to use checksums
3acd5085f39867e944ed146aab179eb1c5dc867a718f1ac30fb1bbc8e75e3818
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.12.1 pkginfo/1.4.2 requests/2.21.0 setuptools/40.6.3 requests-toolbelt/0.8.0 tqdm/4.28.1 CPython/3.6.7

Release history Release notifications | RSS feed

This release

0.0.2 This release

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page