views:

52

answers:

1

I have a urlpattern that brings a template that allows the fields of a model instance to be viewed:

(r'^display/(?P<id>\w+)/', display_record),

I also have a view function that allows a single instance to be edited. When the object is saved, it simply returns to the same template:

if form.is_valid():
        form.save()
        return HttpResponseRedirect('/')

After the save, how do I return to the display template, as opposed to refreshing the same view? the code would look something like the following but I need a way to pass the object "id" to the HttpResponse request:

def edit_record(request, id):
        if request.method == 'POST':
                a=ProjectRecord.objects.get(pk=id)
                form = RecordForm(request.POST, instance=a)
                if form.is_valid():
                        form.save()
                        return HttpResponseRedirect**('/display/(?P<id>\w+)/')**
        else:
                a=ProjectRecord.objects.get(pk=id)
                form = RecordForm(instance=a)
        return render_to_response('productionModulewire.html', {'form': form})
+4  A: 

You're overcomplicating things. You already know what the ID is, so why can't you just put it back into the URL?

return HttpResponseRedirect('/display/%s/' % id)

or, better, since it doesn't tie you to a particular hard-coded URL:

return HttpResponseRedirect(reverse('edit_record', kwargs={'id':id}))
Daniel Roseman
Thanks Daniel. That's what I was trying to do. Didn't know the proper syntax. Thanks again.
kjarsenal