diff --git a/handbook/troggle/trogforms.html b/handbook/troggle/trogforms.html index 930d19adf..c07c6fa3a 100644 --- a/handbook/troggle/trogforms.html +++ b/handbook/troggle/trogforms.html @@ -9,8 +9,10 @@

Django Forms History

Django has several generations of quite different clever mechanisms to make creating HTML forms "easier". Yes, making them might be easier, but maintaining this opinionated stuff is a nightmare without adequately educating yourself how the architecture works. This will take time: do not hurry. -

If you are a preofessional Django programmer, read this article for why Class Based Views are not a good fit for the community we have maintaining troggle. - +

If you are a professional Django programmer, read this article for why Class Based Views are not a good fit for the community we have maintaining troggle: + "Just look at the documentation of DetailView. To understand this one class,... 11 methods spread across 5 classes and mixins. Debugging a view or figuring out exactly which method to override to make the view behave in a certain way quickly becomes a case of opening way too many files and jumping back and forth between different method declarations. It’s just too much". + If you are still not convinced, read this much longer article. +

WARNING: when reading the Django documentation on Forms (unbound and bound) and ModelForms, do not get diverted into looking at Formsets or ModelFormsets. We do not use any formsets in troggle - of any kind[*].

Django 'Form' object

@@ -41,9 +43,50 @@ But if you haven't worked with HTML forms before, then you actually have So that's enough to get you started. Now you are on your own, apart from help on the Website room of the Matrix chat and the nerd email list of course. -

ModelForms

+

ModelForms

A few of our data entry pages use ModelForms, these are where the Form object is automagically created from a Model class. If you can't find where something is initialised, it is probably because it was done automatically and invisibly by instantiating a ModelForm. - + +

Form views: the code that handles form input

+

The python code which handles the presentation of a form and handles the user responses is called a Django "View" and in troggle these functions are all in python files in the folder troggle/core/views/. There are two ways of doing it: Class-based views (CBV) and Function-based views (FBV). Troggle only uses function-based views for the reasons described very well in this article on why Class Based Views are not a good fit for the community we have maintaining troggle. + +

The documented method for handling the "GET" and "POST" http interactions in Django FBVs are to use a large if statement: +

if request.method == "POST": # POST
+    ....
+else: # GET
+    ....
+
+We use a different layout as these if statements can get very deep and long. We use a layout like this (simplified): +
def dwgupload(request, folder):
+
+    def _setup(folder_arg):
+        # initialize common state and return context dictionary ctx
+        ......
+        return ctx
+    def _get(ctx):
+        # build page and prepare form using data in the supplied context ctx
+        ......
+        return ctx
+    def _post(ctx):
+        # handle POST -- validate form and save files, update ctx in-place
+        ......
+        return ctx
+
+   # main flow: setup, then POST or GET handlers, then render
+    ctx = _setup(folder)
+
+    if request.method == "POST":
+        ctx = _post(ctx)
+    else:
+        ctx = _get(ctx)
+
+    response = render(request, .....) # rendered form to display on screen
+
+ +

+ Not all our forms use this structure yet. +

+ We looked at using the extremely simplified Class-based View structure where all our view functions would inherit from Django View class, but it has no advantages over this pure function structure and many disadvantages in readability and maintainability. +

For loop