How it works...

In the initialization method of MessageForm, we have the self variable that represents the instance of the form itself, we also have the newly added request variable, and then we have the rest of the positional arguments (*args) and named arguments (**kwargs). We call the super() initialization method, passing all of the positional and named arguments to it so that the form is properly initiated. We will then assign the request variable to a new request attribute of the form for later access in other methods of the form. Finally, we modify the queryset attribute of the recipient's selection field, excluding the current user from the request.

In the message_to_user view, we will pass the HttpRequest object as the first argument in both situations:

  • When loaded for the first time
  • When the form is posted

The form is rendered via the given message_to_user.html template, which prints out only the markup for the form itself in our example here. In the real world, this would probably extend from a base.html template as described in the Arranging the base.html template recipe in Chapter 4Templates and JavaScript. With our basic markup, this would look something like the following once filled in:

After submission completes successfully, we redirect to the message_sent named URL, which maps back to the message_sent view. In this, we simply render a message via the message_sent.html template, something like this:

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
13.58.247.31