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?

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.

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?

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).

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

# In models.py:

class Data(models.Model):
     experiment_id = models.CharField(maxlength=10)
     procedure_id  = models.CharField(maxlength=10)
     field_name    = models.CharField(maxlength=255, core=True)
     value         = models.CharField(maxlength=255, core=True)

# In views.py

def test(request):

     # form_for_model() returns a Form class, not a Form instance. You have to
     # instantiate the form class before sending it to the template, like so:
     #   SomeForm = forms.form_for_model(Project)
     #   form = SomeForm()
     #   return render_to_response('app/my.html', {'form': form})

     DataForm = forms.form_for_model(Data)

     if request.method == 'POST':
         form = DataForm(request.POST)
         if form.is_valid():
             form.save()
             return HttpResponseRedirect("/")
     else:
        form = DataForm()

     data = {'Procedure': 'test',
             'Field': 'temperature',
             'Value': '20C',
             'form': form,
            }

     return render_to_response('lab/test.html', data)

# In test.html

<form action="." method="post">
   <table>
     {{ form }}
   </table>
   <input type="submit" value=" Submit " />
</form>

Also I'm confused on the save methods. Some exemples use form.save() and others 
define a save method under the model Class.

Sorry this is a bit of a long post with many questions. The whole newforms just 
has 
not yet "clicked" :-)

-- 
Michael Lake




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