FAQ
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

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To view this discussion on the web visit https://groups.google.com/d/msg/django-users/-/b3rgnekl6asJ.
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.

Search Discussions

  • Kevin at Jul 14, 2012 at 5:53 am
    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
    On Friday, July 13, 2012 3:00:03 PM UTC-5, Kevin wrote:

    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
    --
    You received this message because you are subscribed to the Google Groups "Django users" group.
    To view this discussion on the web visit https://groups.google.com/d/msg/django-users/-/YjQRiYfGyokJ.
    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.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupdjango-users @
categoriesdjango, python
postedJul 13, '12 at 8:00p
activeJul 14, '12 at 5:53a
posts2
users1
websitedjangoproject.com

1 user in discussion

Kevin: 2 posts

People

Translate

site design / logo © 2022 Grokbase