开发者

Returning user data for forms that have errors in when using ModelForms

forms.py

from django.forms import ModelForm
from client.models import ClientDetails, ClientAddress, ClientPhone
from snippets.UKPhoneNumberForm import UKPhoneNumberField

class ClientDetailsForm(M开发者_运维知识库odelForm):
    class Meta:
        model = ClientDetails

class ClientAddressForm(ModelForm):
    class Meta:
        model = ClientAddress

class ClientPhoneForm(ModelForm):
    number = UKPhoneNumberField()

    class Meta:
        model = ClientPhone

views.py

from django.shortcuts import render_to_response, redirect
from django.template import RequestContext
from client.forms import ClientDetailsForm, ClientAddressForm, ClientPhoneForm

def new_client_view(request):
    formDetails = ClientDetailsForm(initial={'marital_status':'u'})
    formAddress = ClientAddressForm()
    formHomePhone = ClientPhoneForm(initial={'phone_type':'home'})
    formWorkPhone = ClientPhoneForm(initial={'phone_type':'work'})
    formMobilePhone = ClientPhoneForm(initial={'phone_type':'mobi'})
    return render_to_response('client/new_client.html', {'formDetails': formDetails, 'formAddress': formAddress, 'formHomePhone': formHomePhone, 'formWorkPhone': formWorkPhone, 'formMobilePhone': formMobilePhone}, context_instance=RequestContext(request))

new_client.html

<form action="." method="POST">
    <table>
        <tr>
            <td>
                <table>
                    {{ formDetails.as_table }}
                    {{ formAddress.as_table }}
                </table>
            </td>
            <td>
                <table>
                    {{ formHomePhone.as_table }}
                    {{ formWorkPhone.as_table }}
                    {{ formMobilePhone.as_table }}
                </table>
            </td>
        </tr>
    </table>
    <input type="submit" value="Submit">
</form>

How should I write views.py so that if the user's data raises an error, instead of showing them the form again with the errors in but none of their original data, it shows them the form again with the errors AND their original data?


As MYYN says, look here: http://docs.djangoproject.com/en/dev/topics/forms/#using-a-form-in-a-view

ModelForms are Forms too and using them is no different.

if request.method == 'POST': # If the form has been submitted...
    form = ContactForm(request.POST) # A form bound to the POST data
    # now your form contains the values submitted
else:  
    form = ContactForm() # empty form

The django forms should always return the errors AND their originally submitted data.

The only way you would get errors and not their data is if you did something like passing an empty dictionary to the form.

Your example would never show any errors as you are never passing it data to validate against. It would always show an empty form.

Initial is not the place to pass in user submitted data

f = Form(initial={'foo':'bar'})
f.is_valid()
False
f.errors # will not validate or show you errors

Pass in the POST dictionary instead

f = Form({'foo':'bar'})
f.is_valid()
False
f.errors
{'baz': [u'This field is required.']}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜