Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>If these variables are intended to populate a form, then you can safely pass the <code>request.POST</code> object directly into the form constructor.</p> <pre><code>if request.method == 'POST': form = MyForm(request.POST) </code></pre> <p>The form will automatically pass the correct values to the correct form fields <strike>and use defaults for keys that don't exist</strike> and will still create blank fields for missing keys (see addendum). </p> <p>If you are trying to process a form, it is still better to create a form object as above, and read out the values from that object.</p> <pre><code>if request.method == 'POST': form = MyForm(request.POST) if form.is_valid(): # You may process these variables here print form.appVersion print form.appName print form.appCodeName </code></pre> <p>Remember, validation code is best placed in the form class as well. That way, if <code>form.is_valid()</code> returns <code>True</code>, then you know you have a clean dataset to work with.</p> <p>Note: Django docs recommend using <code>request.POST</code> or <code>request.GET</code> directly rather than the amalgamated variable <code>request.REQUEST</code>, as it is more explicit.</p> <p><strong>Addendum:</strong></p> <p>It is important to understand the difference between bound and unbound forms in this case. If you create an unbound form with <code>form = MyForm()</code>, then when the form is instantiated, it will fill in all fields with the <code>initial</code> property of each field (if it exists). For example, with this code: </p> <pre><code>from django import forms class MyForm(forms.Form): appVersion = forms.CharField(initial='1.0') appName = forms.CharField() appCodeName = forms.CharField() </code></pre> <p>the form will be initialized with appVersion having a value of '1.0'. However, if you bind a POST request to a form like this: <code>form = MyForm(request.POST)</code>, then the initial properties are ignored. That means if the POST dict does not include an appVersion key, then that field will be left blank. As long as the field is not required, your form will still validate, and you can modify <code>form.appVersion</code> in the view after validation. </p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload