Snippet List
If you require lots of forms in your project and do not want to be creating an extended template for each one I propose this solution.
Classes in the html correspond to bootstrap, you can work without them if you do not use bootstrap.
- template
- django
- dynamic
- fields
- forms
- generic
- generic-views
- html
- form
- bootstrap
- reusable
- custom
- modelform
- crud
- dynamic-form
This example assumes you have a form and want to highlight one of two fields
by setting <class="highlight"> in the html dynamically. This is an alternative to
<https://docs.djangoproject.com/en/1.3/ref/forms/widgets/#customizing-widget-instances>,
but now you're not limited to assigning the class to the fields html-output,
instead you can also assign it to a div around the field like done here.
After assigning a css-attribute to a field, we access the css via a templatefilter
*{{ field|css }}*
that looks up
*field.form.fields[field.name].css*
and not simply *field.css*, since the latter would try to access
a non-existing css-attribute on a BoundField-instance
EDIT:
The templatefilter is unnecessary. There is a much easier way, since the original field itself is an attribute of the BoundField named 'field'. So in the template, we can access the css via {{ field.field.css }}. Thanks to Tom Evans for pointing me at this.
- css
- form
- class
- dynamic-form
- formfield
- dynamic-css
- css-class
This is a little snippet that you can use to make your Django newforms dynamic at runtime. You can define an arbitrary number of fields of the form without loosing newforms capibilites.
You can render the form dynamically be "misusing" the "help_text" attribute of the form fields (Every form field allows the definition of this attribute). This way you can search for changes in the help_text of every field and render the form accordingly.
The form itself is dynamically defined in the view.
The form state can be saved in a Django Session Variable (specifically for Linux users with a process-based Apache Server), so that you can rebuild and verify a submitted form.
- newforms
- dynamic-form
- runtime
- custom-forms
3 snippets posted so far.