Upgrade to Pro — share decks privately, control downloads, hide ads and more …

“Forms are static” – “No, they aren't”

Cd7648c536b4dbe940246b74044fbc52?s=47 Markus H
June 02, 2015

“Forms are static” – “No, they aren't”

My talk from DjangoCon Europe 2015 in Cardiff, Wales

Cd7648c536b4dbe940246b74044fbc52?s=128

Markus H

June 02, 2015
Tweet

Transcript

  1. “Forms are static” – “No, they aren't”

  2. Hi, I’m Markus Holtermann • Django Core Developer • CS

    MSc Student @m_holtermann • github.com/MarkusH • markusholtermann.eu
  3. Forms are static

  4. What does “static” even mean?

  5. What about request dependent forms?

  6. They are static too!

  7. What are non-static forms then?

  8. Why do you need them?

  9. How does that work?

  10. def modelform_factory(model, …): attrs = { ‘fields’: fields_for_model(model), ‘model’: model,

    } Meta = type(str('Meta'), (object,), attrs) class_attrs = {‘Meta’: Meta} class_name = model.__name__ + str('Form') return type( class_name, ModelForm, class_attrs )
  11. Wait! Aren’t they still static?

  12. type(name, bases, attrs) rocks!

  13. django-dynamic-forms http://django-dynamic-forms.rtfd.org

  14. Actions FormModel Name URLs Name FormFieldModel Options Label … …

    belongs to
  15. @formmodel_action('Send to admins') def send_admins(form_model, form, request): # label →

    value mapped_data = form.get_mapped_data() # construct message message = render_to_string( 'dynamic_forms/email.txt', { 'form': form_model, 'data': mapped_data}) # send data via mail mail_admins( “New form submission”, message )
  16. @dynamic_form_field class IntegerField(BaseDynamicFormField): cls = 'django.forms.EmailField' display_label = 'Email' class

    Meta: # attribute = [type, default, class] max_value = [int, None, forms.IntegerField] min_value = [int, None, forms.IntegerField]
  17. None
  18. None
  19. None
  20. django-dynamic-forms http://django-dynamic-forms.rtfd.org

  21. Thank You! @m_holtermann • github.com/MarkusH • markusholtermann.eu