Not really a DRY issue moreover there is an easier logic to your code: if request.method == 'POST': form = MyForm(request.POST) if form.is_valid(): # make new model object and save return HttpResponseRedirect('/') else: form = MyForm(initial=data) return render_to_response('template.html', RequestContext(request, { 'foo': foo, 'bar': bar }))
The docs call this a simple view example: http://www.djangoproject.com/documentation/newforms/#simple-view-example Hope that helps, Michael On Mon, Mar 31, 2008 at 9:54 PM, msoulier <[EMAIL PROTECTED]> wrote: > > I find myself doing this a lot in view code > > if request.method == 'GET': > form = MyForm(initial=data) > return render_to_response('template.html', > RequestContext(request, { > 'foo': foo, > 'bar': bar > })) > > elif request.method == 'POST': > form = MyForm(request.POST) > if form.is_valid(): > # make new model object and save > else: > # same as the GET method code to display errors > > The issue is that if the form validation fails, I end up using the > same code to display the page. > > Is there a better way? I could put the template render in a function, > but I'm not sure how much that would save. What do the rest of you do? > > Thanks, > Mike > > > --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~----------~----~----~----~------~----~------~--~---