Incorrect post URL on invalid form submissions

39 views
Skip to first unread message

Kevin

unread,
Jul 13, 2012, 4:00:03 PM7/13/12
to django...@googlegroups.com
I'm new to Django so hopefully this will be trivial to solve.

I have a table of data I display in a view along with a simple form to add a new row of data to the table:

def my_data_view(request, data_id):
    myData = MyData.objects.get(pk=data_id)
    if request.method == 'POST':
        myForm = MyForm(request.POST)
    else:
        myForm = MyForm()
    return render_to_response('myapp/mydata.html',
                              { 'my_data' : myData,
                                'my_form' : myForm,},
                              context_instance=RequestContext(request))      
      
def add_new_row(request, data_id):
    myData = MyData.objects.get(pk=data_id)
    if request.method == 'POST':
        myForm = MyForm(request.POST)
        if myForm.is_valid():
            # TODO insert new time into DB
            return HttpResponseRedirect(reverse('myapp.views.mydata', args=(myData.id,)))
    return my_data_view(request, data_id)

This works when I submit a valid form. However submitting an invalid form directs me from myapp/mydata/3 to myapp/mydata/addNewRow/3 which means when I submit the corrected form it posts to myapp/addNewRow/addNewRow/3 which is obviously not what I want. Any suggestions?

Thanks much!

Kevin

Kevin

unread,
Jul 14, 2012, 1:52:53 AM7/14/12
to django...@googlegroups.com
I refactored this into a single view (similar to the Django form example) and it's working fine...not to mention it's much more DRY.

Feel free to disregard!

Kevin
Reply all
Reply to author
Forward
0 new messages