{ "info": { "author": "Anders Hovm\u00f6ller", "author_email": "anders.hovmoller@trioptima.com", "bugtrack_url": null, "classifiers": [ "Development Status :: 5 - Production/Stable", "Intended Audience :: Developers", "License :: OSI Approved :: BSD License", "Natural Language :: English", "Programming Language :: Python :: 3", "Programming Language :: Python :: 3.6" ], "description": ".. image:: https://travis-ci.org/TriOptima/tri.form.svg?branch=master\n :target: https://travis-ci.org/TriOptima/tri.form\n\n.. image:: http://codecov.io/github/TriOptima/tri.form/coverage.svg?branch=master\n :target: http://codecov.io/github/TriOptima/tri.form?branch=master\n\n\ntri.form\n==========\n\ntri.form is alternative forms library for Django and Flask. It is inspired by, and comes from a frustration with, the standard Django forms.\n\nMajor features compared to Django forms:\n\n- Supports :code:`__` syntax for going across table/object boundaries, similar to how Django does with QuerySets.\n- Send in a callable that is late evaluated to determine if a field should be displayed (:code:`show`). This is very handy for showing a slightly different form to administrators for example.\n- Easy configuration without writing entire classes that are only used in one place anyway.\n\n\nExample\n-------\n\nYou can either create a subclass of :code:`Form`...\n\n.. code:: python\n\n class UserForm(Form):\n name = Field.text()\n username = Field.text(is_valid=lambda form, field, parsed_data: parsed_data.startswith('demo_'))\n is_admin = Field.boolean(\n show=lambda form, field: form.request.user.is_staff, # show only for staff\n label_template='tweak_label_tag.html')\n\n def edit_user_view(request, username):\n form = UserForm(request=request)\n\n user = User.objects.get(username=username)\n if form.is_valid() and request.method == 'POST':\n form.apply(user)\n user.save()\n return HttpRedirect('..')\n\n return render(\n template_name='edit_user.html',\n context_instance=RequestContext(request, {'form': form}))\n\n.. code:: html\n\n \n
\n\nor just instantiate a :code:`Form` with a :code:`Field` list and use it directly:\n\n.. code:: python\n\n def edit_user_view(request, username):\n form = Form(fields=[\n Field.text(\n name='name',\n is_valid=lambda form, field, parsed_data: parsed_data.startswith('demo_')),\n Field.text(name='username'),\n Field.boolean(\n name='is_admin',\n show=lambda form, field: form.request.user.is_staff, # show only for staff\n label_template='tweak_label_tag.html',)])\n\n # rest of view function...\n\n\nYou can also generate forms from Django models automatically (but still change the behavior!). The above example\nis equivalent to:\n\n.. code:: python\n\n def edit_user_view(request, username):\n form = Form.from_model(\n request.POST,\n User,\n # the field 'name' is generated automatically and we are fine with the defaults\n username__is_valid=lambda form, field, parsed_data: parsed_data.startswith('demo_'),\n is_admin__label_template='tweak_label_tag.html',\n is_admin__show=lambda form, field: form.request.user.is_staff) # show only for staff\n\n # rest of view function...\n\nor even better: use :code:`tri.form.views.create_or_edit_object`:\n\n.. code:: python\n\n def edit_user_view(request, username):\n return create_or_edit_object(\n request,\n model=User,\n is_create=False,\n instance=User.objects.get(username=username),\n\n form__username__is_valid=lambda form, field, parsed_data: parsed_data.startswith('demo_'),\n form__is_admin__label_template='tweak_label_tag.html',\n form__is_admin__show=lambda form, field: form.request.user.is_staff) # show only for staff\n # no html template! tri.form has a nice default for you :P\n\ntri.form pre-packages sets of defaults for common field types as 'shortcuts'. Some examples include :code:`Field.boolean`,\n:code:`Field.integer` and :code:`Field.choice`. The full list of shortcuts can be found in the `API documentation for Field