Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>It sounds like you may be confused about the difference and proper usage of <code>Form</code> vs <code>ModelForm</code></p> <p>Regardless of which type of form you use, the templating side of forms stays the same: Note: all of the values in your form (as long as its bound to POST or has an instance) will be prepopulated at render.</p> <pre><code>&lt;form class="well" action="{% url member-profile %}" method="POST" enctype="multipart/form-data"&gt;{% csrf_token %} &lt;fieldset&gt; {{ form.non_field_errors }} {{ form.display_name.label_tag }} &lt;span class="help-block"&gt;{{ form.display_name.help_text }}&lt;/span&gt; {{ form.display_name }} &lt;span class="error"&gt;{{ form.display_name.errors }}&lt;/span&gt; {{ form.biography.label_tag }} &lt;span class="help-block"&gt;{{ form.biography.help_text }}&lt;/span&gt; {{ form.biography }} &lt;span class="error"&gt;{{ form.biography.errors }}&lt;/span&gt; &lt;input type="submit" class="button primary" value="Save" /&gt; &lt;/fieldset&gt; &lt;/form&gt; </code></pre> <p>if you want to be populating a form from a record (or submit a form as a record) its probably best to use <code>ModelForm</code></p> <p>EX a profile form that doesn't display the User FK dropdown:</p> <pre><code>class ProfileForm(forms.ModelForm): """Profile form""" class Meta: model = Profile exclude = ('user',) </code></pre> <p>The View:</p> <pre><code>def profile(request): """Manage Account""" if request.user.is_anonymous() : # user isn't logged in messages.info(request, _(u'You are not logged in!')) return redirect('member-login') # get the currently logged in user's profile profile = request.user.profile # check to see if this request is a post if request.method == "POST": # Bind the post to the form w/ profile as initial form = ProfileForm(request.POST, instance=profile) if form.is_valid() : # if the form is valid form.save() messages.success(request, _(u'Success! You have updated your profile.')) else : # if the form is invalid messages.error(request, _(u'Error! Correct all errors in the form below and resubmit.')) else: # set the initial form values to the current user's profile's values form = ProfileForm(instance=profile) return render( request, 'membership/manage/profile.html', { 'form': form, } ) </code></pre> <p>notice that the outer <code>else</code> initializes the form with an instance: <code>form = ProfileForm(instance=profile)</code> and that the form submit initializes the form with post, BUT still binds to instance <code>form = ProfileForm(request.POST, instance=profile)</code></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