On Thu, May 8, 2008 at 1:00 AM, leppy <[EMAIL PROTECTED]> wrote: > > Hi everyone, > > I have a model 'Person' in my application as follows: > > class Person(models.Model): > name = models.CharField(max_length = 100) > address = models.CharField(max_length = 800) > phone = models.CharField(max_length = 15) > > Next, I have created a modelform, > > class PersonForm(ModelForm): > address = forms.CharField(widget = forms.Textarea(attrs = {'rows': > 3, > 'cols':30 })) > class Meta: > model = Person > > > > views.py > ******** > > def getInfo(request): > person = Person() > if request.method == POST: > pform = PersonForm(request.POST) > if pform.is_valid(): > pform.save() > else: > pform = PersonForm(instance = person) > else: > pform = PersonForm() > return render_to_response("person.html",{"form":pform}) > > > My problem is that when pform.is_valid() is 'false', I want to reprint > the form with user submitted data and also with error messages for > corresponding fields with errors. But now a blank form is being > rendered. >
If you want to display the entered data with error messages, you simply display the form that failed validation. So remove the else leg of if pform.is_valid(), where you replace the form that failed validation with a blank one. (It's also common practice to return an HttpResponseRedirect in the case where the post and save was successful, which you don't seem to be doing, meaning the input form is going to be re-displayed even in the case where there were no errors.) What you want is something more like: def getInfo(request): if request.method == POST: pform = PersonForm(request.POST) if pform.is_valid(): pform.save() return HttpResponseRedirect('/your_url_for_successful_post') else: pform = PersonForm() return render_to_response("person.html",{"form":pform}) Karen --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---