4

In my Django app I have a listing of items with a 'delete' option available next to each one. I want the user to be able to click the 'delete' button, have it send a $.post to the Django view, where it will delete that instance. Upon successfully deleting this instance, i'll trigger another Ajax request in javascript to redraw the listing without the deleted instance.

My question: if I'm not actually returning a value to the $.post, then should I be using ajax to do this operation? Is Ajax specifically for retrieving information? And is a Django view required to return an HttpResponse even if I'm not actually returning anything?

What is the proper way to handle something like a 'delete' request and view?

Thanks.

1 Answer 1

4

A Django view always has to return an HttpResponse object, or one of it's subclasses. Just try returning something else and see how Django reacts (it should cause a 500 error).

You can use the $.post method in jQuery for the situation you described. If you do you should definitely return a string to indicate a successful deletion or an error. If you do not take an error into account you could end up removing something from the page that the user is viewing without actually deleting the item on the server, which could cause inconsistency and confusion for you users.

Your UI should then have a specific state for objects that are being deleted - that is, the user has pressed delete but the server hasn't answered yet*. Then if there is an error (you either receive a string indicating an error in the success function, or the error function is invoked, see the jQuery docs about $.post) you can report it to the user and return the item to it's normal state. If the deletion is successful you can proceed to remove the item from the page (or do whatever you intended to do when it had been deleted).

*(A good indicator of an item being deleted could be to reduce it's opacity (make it a little bit see-through) and add a little spinning wheel or another indicator of the deletion being "in process".)

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

Comments

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.