Thanks Tim for your detailed reply.  I have it now.  HTML was
incorrect, and yes, now I understand better what is happening I have
rewritten the view as you suggest.  I split it up only to try to make
clearer to me what was happening.  Thanks again.

Mike

On Jun 2, 1:48 am, Tim Sawyer <list.dja...@calidris.co.uk> wrote:
> Hi,
>
> I think the recommended approach in django is for forms to submit to the
> current URL and for the view to decide whether the request is to display the
> form (a HTTP GET) or submit the form (HTTP POST).  I'd suggest that you
> attempt to re-write this way.  
>
> Your error could be caused by nothing in the POST, if your form is wrong - can
> you post your HTML?  You could also put an intentional error into your view
> which will cause the Django error window to pop up - this contains details of
> the POST variables in the request.
>
> Here's a well commented and cleaner version of the code I posted earlier:
>
> def edit_result(request, pResultSerial):
> """
> This method in views.py takes a single parameter from the url - the serial of
> an object.  It will either display the HTML form to edit that object, or it
> will accept the submit of a form containing the edited data and save it to the
> database
> """
>   # Get an object from the database, using the passed in serial.  Raise a 404
>   # page not found if the object isn't found
>   lContestResult = get_object_or_404(ContestResult, pk=pResultSerial)
>
>   # if we are doing a post, the we have data to save.  Process it.
>   if request.method == 'POST':
>
>     # create a form instance, populating it with the data in the object
>     # selected from the database earlier, then overwriting it with the
>     # stuff submitted in the HTML form
>     form = ContestResultForm(request.POST, instance=lContestResult)
>
>     # run the form validation
>     if form.is_valid():
>       # save the object inside the form instance to the database
>       form.save()
>
>       # our save completed, so redirect to the next url you want to go to
>       return HttpResponseRedirect('/url/after/successful/save')
>
>   else:
>     # we aren't doing a POST, so we need to create a form
>     # instance ready for it to be edited in the HTML.
>     # we create this and populate it from the object we selected above
>     form = ContestResultForm(instance=lContestResult)
>
>   # show the HTML form to allow the user to edit the object
>   # note that this is indented to the same level as the
>   # "if request is a POST" check, so that if the form.is_valid() check fails,
>   # we go back and show the HTML again with the form containing errors.
>   return render__to_response('contests/edit_result.html',
>                                 {'form': form})
>
> Hope that helps!
>
> Tim.
>
> On Monday 01 June 2009 15:56:24 adelaide_mike wrote:
>
> > Thanks for your response Tim.  However, you lost me a bit there, I am
> > a real newbie.  I have narrowed my question down to this:
>
> > # in views.py:
>
> > class PropertyForm(ModelForm):
> >    class Meta:
> >            model = Property
>
> > def property_update(request, property_id='0', street_id='0'):
> >    print "data/property_save, request.method= ", request.method
> >    message = ''
> >    # we attempt to update an edit
> >    print "attempt to update"
> >    form = PropertyForm(request.POST)
> >    if form.is_valid():
> >            form.save()
>
> >    return render_to_response('wha/property_form.html', {'form': form,
> > 'message': message})
>
> > My property_update function is called when the form Save button is
> > clicked.  The various "print" commands operate as expected.  However,
> > the validation fails and a form with no data is returned with
> > "required data" labels.  I conclude the line:
> > form = PropertyForm(request.POST)
> > does not populate the validation form.  What have I got wrong here?
> > TIA
>
> > Mike
>
> > On Jun 1, 8:14 pm, Tim Sawyer <list.dja...@calidris.co.uk> wrote:
> > > On Monday 01 June 2009 01:38:30 adelaide_mike wrote:
> > > > I found a really clear explanation of creating and updating database
> > > > objects in SAMS TeachYourself Django, but it appears to be for v
> > > > 0.96.
>
> > > > I have looked at "Creating forms from models" in the documentation,
> > > > and about one-third the way down it shows the following:
>
> > > > # Create a form instance from POST data.
>
> > > > >>> f = ArticleForm(request.POST)
>
> > > > # Save a new Article object from the form's data.
>
> > > > >>> new_article = f.save()
>
> > > > # Create a form to edit an existing Article.
>
> > > > >>> a = Article.objects.get(pk=1)
> > > > >>> f = ArticleForm(instance=a)
> > > > >>> f.save()
>
> > > > # Create a form to edit an existing Article, but use
> > > > # POST data to populate the form.
>
> > > > >>> a = Article.objects.get(pk=1)
> > > > >>> f = ArticleForm(request.POST, instance=a)
> > > > >>> f.save()
>
> > > > I understand what these code fragments are intended to do (I think)
> > > > but I am not clear as to how to use them.  Can someone point me to a
> > > > more fully displayed example?  TIA
>
> > > > Mike
>
> > > Here's an example from my code, does this help?
>
> > > Tim.
>
> > > def edit_result(request, pResultSerial):
> > >     """
> > >     Edit a single result row
> > >     """
> > >     lContestResult = get_object_or_404(ContestResult, pk=pResultSerial)
> > >     if request.user != lContestResult.owner:
> > >         raise Http404()
> > >     if request.method == 'POST':
> > >         form = ContestResultForm(request.POST, instance=lContestResult)
> > >         if form.is_valid():
> > >             form.save()
> > >             return
> > > HttpResponseRedirect(reverse('bbr.contests.views.single_contest_event',
> > > args=[lContestResult.contest_event.contest.slug,
> > > lContestResult.contest_event.date_of_event]))
> > >     else:
> > >         form = ContestResultForm(instance=lContestResult)
>
> > >     return render_auth(request, 'contests/edit_result.html', {'form':
> > > form, 'ContestResult' : lContestResult})
--~--~---------~--~----~------------~-------~--~----~
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