views:

30

answers:

1

Hi,

I have a form MyForm which I update using ajax as the user fills it out. I have a view method which updates the form by constructing a MyForm from request.POST and feeding it back.

def update_form(request):
    if request.method == 'POST':
        dict = {}
        dict['form'] = MyForm(request.POST).as_p()
        return HttpResponse(json.dumps(dict), mimetype='application/javascript')
    return HttpResponseBadRequest()

However, this invokes the cleaning/validation routines, and I don't want to show error messages to the user until they've actually actively clicked "submit".

So the question is: how can I construct a django.forms.Form from existing data without invoking validation?

+1  A: 

Validation never invokes until you call form.is_valid().

But as i am guessing, you want your form filled with data user types in, until user clicks submit.

def update_form(request):
    if request.method == 'POST':
        if not request.POST.get('submit'):
            dict = {}
            dict['form'] = MyForm(initial = dict(request.POST.items())).as_p()
            return HttpResponse(json.dumps(dict), mimetype='application/javascript')
        else:
            form = MyForm(request.POST)
            if form.is_valid():
                # Your Final Stuff
                pass
    return HttpResponseBadRequest()

Happy Coding.

simplyharsh
Thank you, the use of "initial=" was exactly what I needed.
kdt