You were missing to give the definition of Foo. I assume, that you are
using ModelForm http://docs.djangoproject.com/en/dev/topics/forms/modelforms/
to create FooForm, right?

>     model = Foo()
>     form = FooForm(instance=model)

If you did so, you can just skip the instance and replace this lines
by a simple
form = FooForm()

> and some fields (e.g. username and timestamp) that need to be
> populated by the handler, rather than taken from the HTML form (these

You can add these fields after the form has been sent in the following
way

elif request.method == "POST":
    form = FooForm(request.POST)
    new_entry = form.save(commit=False)
    # populate the entry e.g. new_entry.timestamp =
datetime.datetime.now()
    new_entry.save()

Furthermore, you should add validation, so the POST code should look
like this
elif request.method == "POST":
    form = FooForm(request.POST)
    if form.is_valid():
        # do processing and save here


btw: if you want to have a look at the form for debug reason, just do
print form (html output)
print dir(form) (callable methods)
print form.__dict__ (attributes with values as a dict)
and there will be an output to the server terminal window

On 13 Mrz., 23:21, IanSR <ijsto...@crystal.harvard.edu> wrote:
> I have a  model Foo and a corresponding FooForm with the requisite
> inner Meta class and model=Foo.  I want a url example.com/newfoo to
> bring up a blank form to allow the creation of a new Foo instance.  It
> is not obvious to me how this should be done.  I reckon it is
> something very roughly like:
>
> project/urls.py:
> ===========
> urlpatterns = patterns("", (r"/newfoo", project.views.newfoo))
>
> project/views.py:
> =============
> def newfoo(request):
>   if request.method == "GET":
>     model = Foo()
>     form = FooForm(instance=model)
>     result = render_to_response("newfooform.html" {"form": form})
>   elif request.method == "POST":
>     form = FooForm(request.POST)
>     form.save()
>
> Does this, broadly speaking, sound correct?  My situation is
> complicated by the fact that Foo contains many-to-many relationships
> and some fields (e.g. username and timestamp) that need to be
> populated by the handler, rather than taken from the HTML form (these
> are in the "exclude" tuple on the form Meta definition).  Right now,
> the form generated by FooForm is not useable.  It contains no
> "verbose_name" labels on the fields, and so is undecipherable.
>
> TIA
>
> Ian
--~--~---------~--~----~------------~-------~--~----~
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 
django-users+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to