It is defintely worth using django where possible to do what it can
(otherwise why use django!). The form stuff can do loads for you (validation
for one).

http://www.djangoproject.com/documentation/modelforms/

and 

http://www.djangoproject.com/documentation/models/model_forms/

Are really useful. If the input tags don't display the way you like, you can
either write your own widgets, or define a class for them and use CSS
(depending on what you want to do).

Here is a really cut down version of what you can do to get django to
display a form from a model (not very well coded but simple).
In your view...

def my_view(request)
    if request.method == 'POST':
        f = ContactForm(request.POST)
        if f.is_valid():
            f.save()
            return HttpResponseRedirect('/home/')
    else:
        f = ContactForm()
    return render_to_response('contact.html', locals())


In the contact.html template

...
<form action="." method = "POST">
    <table>
        {{ f.as_table }
    </table>
    <input type="submit" value="Save" />
    <input type="reset" value="reset" />
</form>
....


That should get you started, but the docs are good and it is worth going
through them.

Of course you don't have to do f.as_table, you can access the fields and
display them where you like, but best to start with the basics to understand
how django works first.

Em


> -----Original Message-----
> From: django-users@googlegroups.com 
> [mailto:[EMAIL PROTECTED] On Behalf Of Will Rocisky
> Sent: 20 August 2008 12:03
> To: Django users
> Subject: Re: how to populate data again after validation
> 
> 
> actually I feel comfortable with custom html stuff.. thats why.
> ooopps yeah that was a typo since I typed everything while am 
> on-the- go :)
> 
> I am new to django so I don't really know what to do.
> could you please a little more?
> thanks
> 
> On Aug 20, 4:55 pm, "Emily Rodgers" <[EMAIL PROTECTED]> wrote:
> > Why are you using your own <input> tags (just out of interest!)? It 
> > might be that you have just provided a cut down version of 
> your code 
> > but from what I see, there are a few reasons why it may not 
> be working properly.
> >
> > It looks like you are only passing the errors to the 
> template, but not 
> > the other form values. If you want the previous data, you 
> will need to 
> > put it in your context.
> >
> > Is any validation working at all, and is it saving any data 
> to the db? 
> > It looks like you are calling is_valid on your POST data (not on an 
> > instance of your form object). If you want to pass your 
> POST data to 
> > the form, you need to do something like this:
> >
> > f = ContactForm(request.POST)
> >
> > Or if you need to put more data in (or munged data):
> >
> > contact_instance = Contact(att=value)
> > f = ContactForm(request.POST, instance=contact_instance)
> >
> > HTH
> >
> > Em
> >
> > > -----Original Message-----
> > > From: django-users@googlegroups.com
> > > [mailto:[EMAIL PROTECTED] On Behalf Of Will Rocisky
> > > Sent: 20 August 2008 11:24
> > > To: Django users
> > > Subject: Re: how to populate data again after validation
> >
> > > I am just using...
> >
> > > model...
> > > class Contact(models.Model):
> > >    Name = models.CharField(max_length=100)
> > >    email = models.CharField(max_length=100)
> > >    comments = models.TextField(blank=False)
> >
> > > form...
> > > class ContactForm(ModelForm):
> > >    Name = forms.CharField(label='Name')
> > >    email = forms.EmailField(label='Email')
> > >    comments =
> > > forms.CharField(label='Comments',widget=forms.Textarea)
> >
> > >    class Meta:
> > >            model = Contact
> >
> > > views...
> > > f = ContactForm()
> > > f = request.POST
> > > if f.is_valid():
> > >     f.save()
> > >     return HttpResponseRedirect('/home/')
> > > else:
> > >     ctx = {}
> > >     ctx['errors'] = f.errors
> > >     return render_to_response('contact.html',ctx)
> >
> > > when it doesn't save because validation failed, it renders 
> > > contact.html again, but blank. All previous data lost.
> >
> > > On Aug 20, 4:07 pm, "Emily Rodgers" <[EMAIL PROTECTED]> wrote:
> > > > Hello,
> >
> > > > How are you doing your validation? Is the form 
> populating a model 
> > > > instance or doing something else?
> >
> > > > I would be helpful to see some of the function in your 
> views file.
> >
> > > > Em
> >
> > > > > -----Original Message-----
> > > > > From: django-users@googlegroups.com 
> > > > > [mailto:[EMAIL PROTECTED] On Behalf Of 
> Will Rocisky
> > > > > Sent: 20 August 2008 10:45
> > > > > To: Django users
> > > > > Subject: how to populate data again after validation
> >
> > > > > I am not using builtin {{form}} to print form in 
> template. I am 
> > > > > using my own <input> things.
> > > > > Now, after validation, when it shows error and comes back
> > > to form,
> > > > > all the data has disappeared.
> > > > > How can I keep data in fields even after validation.
> >
> > > > > I am using render_to_response
> > 
> 



--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---

Reply via email to