Hi Michael, On Fri, 2007-03-02 at 16:08 +1100, Michael Lake wrote: > Hi all > > Im trying to understand newforms and comparing the newforms docs at > http://www.djangoproject.com/documentation/newforms/ with "Using djangos > newforms" at > http://code.pui.ch/2007/01/07/using-djangos-newforms/ and Mike Cantelon's > code at > http://www.mikecantelon.com/?q=node/22
> Django's Newforms Docs > ---------------------- > In the newforms docs you subclass Form with "class ContactForm(forms.Form):" > to > create the form object. > 1. Should this be in models.py or views.py ? > 2. Then one instantiates the class f = ContactForm() but is this in view.py > or models.py? Well, you can get away with anything if you try hard enough, but let's work from a "suggested best practice" direction. Everything below is predicted by "I/we recommend...". :-) Forms are view-level functions: a model does not need to be involved with forms, or accessible via the web, so you can usually arrange for things in models.py to be completely divorced from any knowledge of a "request" object and the like. Forms, on the other hand, are your interface for user-input -- very much view functions. So put form handling code in views.py (or modules used by views.py). > > Philipp Keller's Example > ------------------------ > In the "Using djangos newforms" Philipp Keller uses in models.py > class Entry(models.Model): > etc... > > But then he defines this method: > def add_entry(request): > EntryForm = forms.models.form_for_model(Entry) > etc... > > and I'm not sure where this belongs ... in models.py or views.py. It's using "request" -- so it's a view. > > Mike Cantelon's Example > ----------------------- > This is more clear about which file should contain what code. > "ContactForm = forms.form_for_model(Contact)" > is in views.py > > Is form_for_model always called from a def within views.py? I would say yes, usually. Forms are used in views. > > Both Philipp Keller and Mike Cantelons code uses form_from_model so its hard > to > follow and compare with the Django newforms docs. (Thanks though to Mike and > Philipp > for placing their code up). Agree with the thanks -- having a few people write up how they wrestled various early problems into the ground is infinitely useful. > > What I have got so far > ---------------------- > > I have a small test form working using form_for_model and I have placed the > class > statement in models.py [... example snipped...] This looks reasonable. Are you having problems with it misbehaving, or just posting it in case there is some egregious mistake that jumps out? > Also I'm confused on the save methods. Some exemples use form.save() and > others > define a save method under the model Class. They serve slightly different purposes. A model class may have specific things that need to be done upon saving. Now, again, because forms and web-interfaces in general are not the only way to create and update model instances, some of the saving logic definitely belongs to the model class. On the other side, when you automatically create a form from a model (using form_for_model(), it creates a special "save" method that is really a proxy for the model's save() method, but also checks that there are no validation errors in the form and updates the model with the forms inputs. So form.save() for a form generated from form_for_model() is just a quick way to update and save from a form to a model. Does that clear things up at all, or just muddy the waters even further? > Sorry this is a bit of a long post with many questions. The whole newforms > just has > not yet "clicked" :-) Newforms is not something that is immediately obvious, but once it does click, I think it seems much easier than the old forms way. I, for one, am pretty impressed with Adrian's design work here. It's worth persisting with trying to get them to work. Thanks for providing a clear description of where you were having trouble reconciling the differences. Citing three sources that you've already read and distilling the problems makes it much easier to answer questions like this. Regards, Malcolm --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---