3

I have a page that has a simple form. When I submit this form I am redirected to the same page with the new objects created. I'd like to add inline links to the right of every object created to delete and edit. Would I do this with django or would I use javascript/AJAX to handle this? I'm just a little confused on the approach that I should take. Any suggestions?

Here's what my view currently looks like:

def events(request):
    the_user = User.objects.get(username=request.user)
    event_list = Event.objects.filter(user=the_user)
    if request.POST:
        form = EventForm(request.POST)
        if form.is_valid():
            form.save()
    else:
        form = EventForm(initial={'user':the_user})
    return render_to_response("events/event_list.html", {
        "form": form,
        "event_list": event_list,
    }, context_instance=RequestContext(request))

1 Answer 1

2

Usually, you would write another view function, e.g. delete_event(request, event_id) and wire it up in urls.py. Inside the delete view, you would use the provided Model.delete() function to remove the object from the database.

The choice whether to use ajax or not is mostly a matter of taste - you would need to issue a request via javascript to a similar function as I described above, which would take care of the logic.

Some additional overhead is present (when using ajax) in terms of updating the page appropriately.

The proper http verb for deletes would be DELETE, but since this is not usually supported out of the box, you'll use POST.

Sign up to request clarification or add additional context in comments.

3 Comments

So something like this? (r^events/delete/(?P<event_id>\d+)/$' 'delete_event')
But the problem with this is I don't want to be redirected to events/delete/. I want to stay on events/. Thing is, requests going to events/ are already tied up to my event view.
I might not fully understand your concern; but you can always redirect after a delete - e.g. to the page you are coming from. Say you are on events, you click on the link which says delete - which takes you to events/1231/delete (not very restful, I know ;) - and then you'll get immediately redirected back to events, this time with one less item in your list - wouldn't that be sufficient?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.