One thing to note is that if your form is 'foo', calling foo.__str__(), foo.as_table() and print foo all do the same thing, ie the table output is the default __str__ output, and in your template, {{foo}} and {{foo.as_table}} will produce exactly the same HTML.
Also, in Emily's view here, the nice thing about this structure is that an invalid form – a form with errors – will get passed back out to the template, where the errors will be displayed. As users keep entering invalid data into the form, this one view structure will keep bouncing the form back out to them, with the data they entered still intact, until they finally get all the fields correct, and the view code can go into the "if foo.is_valid():" branch. This may have been obvious, but if not, it's one of the really nice, tidy things about forms in django... E On Jun 6, 2008, at 12:12 AM, Emily Rodgers wrote: > ah. Right we need to see the view func then ;) as_table is a method > of a form class. You need to call it on an instance of the form > object. > > def myFormFunc(request): > title = 'This is My Form' > if request.method == 'POST': > foo = MyForm(request.POST) # foo is now an instance of > MyForm populated by the post data > if foo.is_valid(): > foo.save() > return HttpResponseRedirect('/yourpath/') > else: > foo = MyForm() # foo is now an instance of MyForm > return render_to_response('template.html', {'foo': foo, 'title': > title}) > > > then in your template you can put > <table> > {{ foo.as_table }} > </table> > does this make it clearer where foo/form comes from? It is just the > variable name you define in the context (the dict that is the second > parameter for the render_to_response function) > > What I did (when it was only displaying my submit button and no > form) was forget to put () on MyForm(). > > Hope this helps. > > Em > > PS if you need to add some (but not all) fields of a model to a > form, and then do some data munging to fill in some of the other > required fields, do shout if you get stuck - this took me a while to > figure out!! > > From: django-users@googlegroups.com [mailto:django-users@googlegroups.com > ] On Behalf Of chris hendrix > Sent: 05 June 2008 16:59 > To: django-users@googlegroups.com > Subject: Re: forms - WTF > > Hi Em - > > I'm taking baby steps at the moment and simply don't understand how > the form.as_table works... ie when i put that in the template (see > my template below), how does it know which form to show? > > > > BR > > On Thu, Jun 5, 2008 at 11:50 AM, Emily Rodgers > <[EMAIL PROTECTED]> wrote: > > Hi, > > It seems familiar to an issue I had last week. > > Can we see the function in your views file? I am guessing that you > need > to instantiate your ziplookup object or something (I think this is > what > I did wrong). Try changing {{ form.as_table }} to {{ form }}, just to > see what it prints... > > I could be worng (I taught myself to code so have plenty of bad > habits!!), but I think the convention when defining classes is to use > CamelCase: http://en.wikipedia.org/wiki/CamelCase for the class name - > it helps to differentiate between function calls and object > instantiations when reading your code. > > Em :) > > PS python rocks. > > > -----Original Message----- > > From: django-users@googlegroups.com > > [mailto:[EMAIL PROTECTED] On Behalf Of Bobby Roberts > > Sent: 05 June 2008 16:36 > > To: Django users > > Subject: forms - WTF > > > > > > hey - > > > > I'm new to Django so bear with me and thank you in advance > > for any help you can lend. I can't seem to get my hands > > around the model/form/ view/template thing. I LOVE the idea > > but you have to understand i'm a MS ASP programmer who is > > switching over to python. > > > > Now with that being said here is my understanding in a nutshell: > > > > 1. models - show the db table structure which is used by the views. > > > > 2. templates - allow you to separate html and code - at > > least for the most part > > > > 3. views - contain the functions which interact with the > > models and templates to perform some sort of interaction. > > > > 4. forms - allow data to be input into the website etc.... > > > > I understand the basics but can't for the life of me figure > > out how to make it all work together in a land of joy and > > joyness (ie Charlie the Unicorn vid) > > > > I'm building a UPS rate lookup service. I am including my > > template and form below. > > > > when i visit the page below, only the input button shows... > > no other form elements > > > > > > [template.html] > > > > {% extends "ups_rates/ups_lookup_base.html" %} {% load i18n %} > > > > {% block content %} > > <div class="ups_rate_lookup_back"></div> > > <div class="ups_rate_lookup_header"> > > <span id="upsheader">{% trans "Estimate UPS Shipping > > Rates" %}</ > > span><br /> > > <div class="upsqueryblock"> > > <form method="post" action="."> > > <table> > > {{ form.as_table }} > > </table> > > <input type="submit" name="submit" value="Get > Estimate*"> > > </form> > > </div> > > </div> > > > > <div class="upsratedata"> > > this is a place holder for my rates table > > </div> > > > > > > <div class="upsdisclaimerbase"></div> > > <div class="upsdisclaimer"> > > {% trans "* UPS rates presented on this page are > > estimates only based solely on the provided zip-code and not > > your full shipping address. Your final shipping rates will > > be displayed on the payment page when you submit your credit > > card information." %} > > </div> > > <div class="upscloser"><form><input type=button > > value="Close Window" onClick="myPopup()"></form></div> {% endblock > %} > > > > > > > > [forms.py] > > > > from django import newforms as forms > > class ziplookup(forms.Form): > > zipcode=forms.CharField(max_length=15, required=True, > > help_text='Please enter your zipcode') > > > > > > > > > > Is there anyone out there who can take me to Candy Mountain > > (another Charlie the Unicorn reference) > > > > > > > > > > > > > > > -- > IMPORTANT NOTICE: The contents of this email and any attachments are > confidential and may also be privileged. If you are not the intended > recipient, please notify the sender immediately and do not disclose > the contents to any other person, use it for any purpose, or store > or copy the information in any medium. Thank you. > > > > > > > IMPORTANT NOTICE: The contents of this email and any attachments are > confidential and may also be privileged. If you are not the intended > recipient, please notify the sender immediately and do not disclose > the contents to any other person, use it for any purpose, or store > or copy the information in any medium. Thank you. > > > > --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---