Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/1stvamp/django-form-scaffold

Utility for generating dynamic Django template representations of the inbuilt form.as_p/as_ul/as_table output.
https://github.com/1stvamp/django-form-scaffold

Last synced: about 17 hours ago
JSON representation

Utility for generating dynamic Django template representations of the inbuilt form.as_p/as_ul/as_table output.

Awesome Lists containing this project

README

        

``django-form-scaffold`` is a package of helper functions for generating Django template code with markup from Django form instances.

It solves the problem of generating, over and over again, markup for a full form which is effectively a templated version of the
``BaseForm`` ``as_p``, ``as_ul`` and ``as_table`` methods output (which output static values, rather than dyanmic Django template code).

``django-form-scaffold`` outputs in the same form as these methods, but uses the template placeholders for all the form and field values,
which is what I find myself doing time and time again as it's easier to control the individual format/styling etc. of fields in a form,
and is also generally easier for designers to get their heads around a form template.

For example, outputting a login form, rather than doing::

{{ form.as_p }}

We would do::


{% if form.username.errors %}
{% for error in form.username.errors %}{{ error }}{% endfor %}
{% endif %}
{{ form.username.label }} {{ form.username }}



{% if form.password.errors %}
{% for error in form.password.errors %}{{ error }}{% endfor %}
{% endif %}
{{ form.password.label }} {{ form.password }}

Requirements
============

* Django >= 1.1

Installation
============

Install ``django-form-scaffold`` using easy_install (or pip)::

easy_install django-form-scaffold

Or from the setup script::

python setup.py install

Usage
=====

Form scaffolding is meant for generating content to then be placed in your template files, so you wouldn't called ``scaffold.as_p``
from within a template itself.

The best way to call the scaffold functions are from within a bootstraped Django Python shell, using the management script::

python manage.py shell

Then just import scaffold from the ``dfs`` namespace, import your form(s), and pass an instance to one of the functions::

>>> from dfs import scaffold
>>> from MyProject.MyApp.forms import MyForm
>>> form = MyForm()
>>> print scaffold.as_p(form)
{% if form.email.errors %}{% for error in form.email.errors %}{{ error }}{% endfor %}{% endif %}

{{ form.email.label }} {{ form.email }}


{% if form.password1.errors %}{% for error in form.password1.errors %}{{ error }}{% endfor %}{% endif %}

{{ form.password1.label }} {{ form.password1 }}


{% if form.password2.errors %}{% for error in form.password2.errors %}{{ error }}{% endfor %}{% endif %}

{{ form.password2.label }} {{ form.password2 }}

>>> # We can also use a form class rather than an instance, but
>>> # this won't always work if your form requires params etc.,
>>> # this just creates an instance for you anyway.
>>> print scaffold.as_ul(cls=MyForm)

  • {% if form.email.errors %}{% for error in form.email.errors %}{{ error }}{% endfor %}{% endif %}{{ form.email.label }} {{ form.email }}

  • {% if form.password1.errors %}{% for error in form.password1.errors %}{{ error }}{% endfor %}{% endif %}{{ form.password1.label }} {{ form.password1 }}

  • {% if form.password2.errors %}{% for error in form.password2.errors %}{{ error }}{% endfor %}{% endif %}{{ form.password2.label }} {{ form.password2 }}
  • The following helper functions available in ``dfs.scaffold``:

    *as_p*
    Outputs the same markup

    style as the inbuilt Django ``BaseForm.as_p``.

    *as_ul*
    Outputs the same markup

      style as the inbuilt Django ``BaseForm.as_ul``.

      *as_table*
      Outputs the same markup style as the inbuilt Django ``BaseForm.as_table``.

      *as_div*
      Extra scaffold-only helper to output an alternative

      based form layout, similar in layout to ``as_p`` but with
      s.

      Management command
      ==================

      As of v1.1.0 *django-form-scaffold* ships with the management command ``formscaffold``, which acts as a simple wrapper around the scaffold functions.

      To use the command just add ``dfs`` to your ``INSTALLED_APPS`` setting and use via ``python manage.py`` (or ``bin/django`` if you're using buildout) like so::

      $ python manage.py formscaffold myapp.forms MyForm as_p

      This just use the class-based generation functionality of scaffold functions (pass in a class and they init a blank instance), if you need any customisations performing to the form instance before hand, use from within a Django-ized Python shell as above (e.g. with ``manage.py shell``).