[Answered ]-Operation of standard Django view, form and template code to update a model

1πŸ‘

βœ…

Ok, So with the help of the references (mentioned below) and the workflow suggested by you, let us first see, the Django MVT workflow and then address the various questions asked in between the post.

WebForms with Django MVT in brief:

  1. Prepare data of several different types for display in a form.
  2. Render data as HTML.
  3. Edit, enter data using a convenient interface.
  4. Validate and clean up the data.
  5. Return data to the server.
  6. Save, delete or pass on for further processing.

The URL:

When a user requests a page from your Django-powered site, this is the algorithm the system follows to determine which Python code to execute. Which is handled by our views.py. From the frontend, if the request is not β€˜POST’, then it is a GET request, hence the else part of the handling function in the views.py executes. This you have mentioned already.

The View: – Form data sent back to a Django website is processed by a view, generally, the same view which published the form. This allows us to reuse some of the same logic. To handle the form we need to instantiate it in the view for the URL where we want it to be published.

If we use request.POST, as in this line:

form = AnyForm(request.POST)

It transforms the form into a bound form (Form bound with data). Read more about it here.

Questioned By You (QBY) – When the submit button is pressed on the displayed page, this "passes back" (??) the {% render_field .. %} values to the view (?????)

So, yes, If the action attribute is not mentioned in the form then the data will be passed to the view responsible for displaying the form.

QBY – the view is executed again (????), but this time request method is set to "POST" because of the form method="POST" in the template (?????)

The button type submit, submits the form and make the request a POST request. The Django template sends that submitted data in the request.POST.

QBY – Now the same form, AnyForm, is "reloaded" (????) but with the parameter value "POST"

Here, if the return method at the end of the POST condition is HttpResponseRedirect it will redirect the user to the mentioned URL page, but if the same HTML is used to be rendered then the form will be displayed as a bound form. (It depends upon the requirements)

QBY – Now if the form is valid (I have no idea what a "valid" or "invalid" form is)

Form.is_valid()

The primary task of a Form object is to validate data. With a bound Form instance, call the is_valid() method to run validation and return a boolean designating whether the data was valid. If yes, then the data is being saved in the model.

QBY – then all the values "captured" in the template by (???)

All the values are sent to views in the request.POST. We can check it by

print(request.POST)

QBY – are written by the view (?????), form.save to the corresponding fields in the ModelForm (?????)

Save method is called on the Django ModelForm instance in order to save the data to the database. Calling save would run the validation check. A ValueError will be raised if the data in the form doesn’t validate.

This saved data can now be processed further.

References: 
[https://docs.djangoproject.com/en/4.0/topics/forms/][2]
[https://www.tangowithdjango.com/book/chapters/models_templates.html][3]
[https://docs.djangoproject.com/en/4.0/ref/forms/api/][4]
πŸ‘€rs_punia

Leave a comment