Thursday, March 29, 2012

Re: Can I POST edit form to detail view, and redirect back if invalid?

On Thu, Mar 29, 2012 at 7:49 AM, Hemebond <hemebond@gmail.com> wrote:
> I don't see anything on there that deals with transferring an
> redirecting with POST data. Is there a particular section that covers
> this?
>
> To be honest, I'm not sure if HTTP really supports what I'm trying to
> do, let alone Django.
>
> On Mar 30, 12:32 am, Joel Goldstick <joel.goldst...@gmail.com> wrote:
>> On Thu, Mar 29, 2012 at 7:13 AM, Hemebond <hemeb...@gmail.com> wrote:
>> > What can I pass back to the edit form view that already contains the
>> > values and errors and how? Posting to the edit form view would work
>> > but that would re-process the form.
>>
>> > On Mar 30, 12:06 am, Joel Goldstick <joel.goldst...@gmail.com> wrote:
>> >> On Thu, Mar 29, 2012 at 6:33 AM, Hemebond <hemeb...@gmail.com> wrote:
>> >> > I want to use the URL of an object (a detail view) as the destination
>> >> > for CRUD operations. I want it to be able to accept GET, POST and
>> >> > DELETE requests.
>>
>> >> > As part of this, I want the edit form (retrieved by appending /edit/
>> >> > to the object URL) to submit to the object URL. If the form is
>> >> > invalid, I then need to redirect back to the edit form, and display
>> >> > the errors.
>>
>> >> > Is this possible? Is there a way to redirect back to the edit form
>> >> > with the new values and the errors that occurred during processing?
>>
>> >> yes
>>
>> You need to look at form handling in django.  Here is a good starting
>> point:http://www.djangobook.com/en/2.0/chapter07/
>>
>> Then look at model forms.
>>
In Django, urlconf doesn't care about whether you are posting or
getting or deleting. It just dispatches to your view. In your view
you check request.method and code accordingly.

I'm just learning Django myself, so others might tear this code apart
but take a look. Whether you come to this url with POST data or not
alters the logic. If POST then validate, and save. If not post, then
fill in the form and display. If no data to fill in, then display a
blank form. The print statements show up on stdout of the terminal
that you start your dev server.

def edit_hotel_profile(request, hotel_id):
hotel_id = int(hotel_id)
print 'Editing hotel profile with hotel_id: %d' % hotel_id
if request.method == 'POST':
print "in edit_hotel_profile with POST data for hotel_id: %d" % hotel_id
#hotel_profile = HotelsHotelProfiles.objects.get(pk=hotel_id)
hotel_profile = HotelProfiles.objects.get(pk=hotel_id)
f = HotelProfileForm(request.POST, instance=hotel_profile)
#hotel_entry = f.save()
print f.errors
#e = f.errors
if f.is_valid():
hotel_entry = f.save()
print hotel_id, "saved to db"
else:
return render_to_response('hotels/hotel_form.html', {'f' :
f,}, context_instance=RequestContext(request))

try:
print "in edit_hotel_profile with no POST data for hotel_id:
%d" % hotel_id
#hotel_profile = HotelsHotelProfiles.objects.get(pk=hotel_id)
hotel_profile = HotelProfiles.objects.get(pk=hotel_id)
f = HotelProfileForm(instance=hotel_profile)
#f.save()
except:
print e
f = HotelProfileForm()
print 'rendering blank form'
return render_to_response('hotels/hotel_form.html', {'f' : f,},
context_instance=RequestContext(request))


>> --
>> Joel Goldstick
>
> --
> 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+unsubscribe@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
>

--
Joel Goldstick

--
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+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.

No comments:

Post a Comment