Multiple forms in django I have an account page where I have three forms. A user can change his name, his email address, and his password.

There are two difficulties I am having from trying to do this:

1) the request.user information is not updating accordingly (e.g., it will lag behind by one change or it will update if the form is not validated)

2) I have three separate forms, however, when I submit one form, I will get validation messages from another form. For example, if I submit the "Change Name" form, I will get 'This field is required from the password fields in the password form. Here is what I currently have -- 

in forms:

# in forms.py
from django.contrib.auth.models import User

class ChangeNameForm(ModelForm):
    first_name = forms.CharField(required=True)
    last_name = forms.CharField(required=True)
    class Meta:
        model = User
        fields = ('first_name', 'last_name' )

class ChangeEmailForm(ModelForm):
    email = forms.EmailField(required=True)
    class Meta:
        model = User
        fields = ('email',)


in views:

@login_required
def account(request):
    name_message = password_message = email_message = ''
    change_name_form = ChangeNameForm(data=request.POST or None, instance=request.user)
    change_password_form = PasswordChangeForm(data=request.POST or None, user = request.user)
    change_email_form = ChangeEmailForm(data=request.POST or None, instance=request.user)
    if request.method == "POST":
        if "change_name" in request.POST and change_name_form.is_valid():
            change_name_form.save()
            name_message = 'Your name has been changed.'
        if "change_password" in request.POST and change_password_form.is_valid():
            change_password_form.save()        
            password_message = 'Your password has been changed.'
        if "change_email" in request.POST and change_email_form.is_valid():
            ...
            email_message = 'Please click the link in your email to confirm changes.'
    return render_to_response('userprofile/account.html', 
                       {'change_name_form': change_name_form,
                        'change_email_form': change_email_form, 
                        'change_password_form': change_password_form,
                        'password_message': password_message,
                        'name_message': name_message,
                        'email_message': email_message,}, 
                        context_instance=RequestContext(request))


in template:

&lt;h3&gt;Change Name&lt;/h3&gt;
    &lt;form method="post" action="/account/change/" name='name'&gt; {% csrf_token %}
        &lt;h4&gt;Change name: {{user.first_name}} {{user.last_name}}&lt;/h4&gt;
        &lt;table&gt;{{change_name_form.as_table}}&lt;/table&gt;
        &lt;p&gt;{{name_message}}&lt;/p&gt;
        &lt;p&gt;&lt;input type="submit" value="Save Changes" name="change_name"/&gt;&lt;/p&gt;
    &lt;/form&gt;

&lt;h3&gt;Change Email&lt;/h3&gt;
    &lt;form method="post" action="/account/change/" name='email'&gt; {% csrf_token %}
        &lt;h4&gt;Change email: {{user.email}}&lt;/h4&gt;
        &lt;table&gt;{{change_email_form.as_table}}&lt;/table&gt;
        &lt;p&gt;{{email_message}}&lt;/p&gt;
        &lt;p&gt;&lt;input type="submit" value="Save Changes" name="change_email" /&gt;&lt;/p&gt;
    &lt;/form&gt;

&lt;h3&gt;Change Password&lt;/h3&gt;
    &lt;form method="post" action="/account/change/" name='password'&gt; {% csrf_token %}
        &lt;h4&gt;Change password&lt;/h4&gt;
        &lt;table&gt;{{change_password_form.as_table}}&lt;/table&gt;
        &lt;p&gt;{{password_message}}&lt;/p&gt;
        &lt;p&gt;&lt;input type="submit" value="Save Changes" name="change_password"/&gt;&lt;/p&gt;
    &lt;/form&gt;


What do I need to do to solve the two issues of making sure the request.user information is current and making sure validation only runs for the current form? Also, would it be possible to run a for loop to reduce redundancy in the template code? If so, how would I do this given the fact that the two name fields?. Thank you.
Somewhere on "Stack Overflow" was question like your.

Main idea was remove None from form initialization.

if "change_name" in request.POST:
    change_name_form = ChangeNameForm(data=request.POST, instance=request.user)
    if change_name_form.is_valid():
        change_name_form.save()
        name_message = 'Your name has been changed.'
else:
    change_name_form = ChangeNameForm(instance=request.user)

if "change_password" in request.POST:
    change_password_form = PasswordChangeForm(user=request.user)
    if change_password_form.is_valid():
        change_password_form.save()        
        password_message = 'Your password has been changed.'
else:
    change_password_form = PasswordChangeForm(user=request.user)

if "change_email" in request.POST:
    change_email_form = ChangeEmailForm(request.POST, instance=request.user)
    if change_email_form.is_valid():
        ...
        email_message = 'Please click the link in your email to confirm changes.'
else:
    change_email_form = ChangeEmailForm(instance=request.user)


This is my solution. I hope it's help you.