R
R
Rrooom2014-08-27 15:15:17
Django
Rrooom, 2014-08-27 15:15:17

How to create your own "pack" of forms in Django?

There are a lot of repeating cruds on the site, the forms for which are very lazy to write-copy. And I want to render my forms with one tag (well, or a filter).
Django has as_t, as_p forms, and crispy_forms has as_div (which is ugly).
Is it possible to create templates for each type of Field (with all the necessary attributes, js widget initializations, etc.), put them somewhere and render a form from them on the page? I saw in the documentation that you can put a widget class in each field, but it’s too not beautiful, and mixing logic is not good in the form handler to write how it looks.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
E
Eugene, 2014-08-28
@TheGhost777

You can try to create one form, and inherit the rest from it, you can immediately rewrite any form methods.
You can write your own form.as_my_form, or render the form in a custom filter.

M
Mikhail Podgursky, 2014-09-04
@kmmbvnr

UPDATE: I moved it to the code in a separate library - https://github.com/viewflow/viewform
Available via pypi: pip install django-viewform
------------------
You can use package django-floppy-forms and define your templates for standard and non-standard widgets - django-floppyforms.readthedocs.org/en/latest/custo...
Unfortunately, as my practice shows. this is often not enough. Immediately after the custom rendering of widgets is done, there are wishes like - "and in this form, render this widget like this, but a little differently", For example, add a bootstrap prepend icon to the input field.
With floppy-forms, you have to get into Python code, which seems like a bit of an overhead.
I started to make my own version, which allows me to override the default rendering of widgets, and, if necessary, change some details in the template of a specific form.
The general idea is like with block inheritance, but blocks are created dynamically and for each field.
And each block can be redefined, inside a specific form template:

{% viewform 'viewform/bootstrap3/form.html' form=form layout=view.layout %}
        {% viewpart field_icon 'my_field_name' %}
        <span class="glyphicon glyphicon glyphicon-pencil form-control-feedback"></span>
        {% endviewpart %}
    {% endviewform %}

This is not yet a separate package, but you can already see how it is implemented here - https://github.com/kmmbvnr/django-viewflow/blob/ma...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question