2

I want to add a feature on my Django project using jQueryUI dialog box where when you click on a link (like a "delete" link) a jQueryUI dialog box will pop up asking you if you really want to delete that item. Then if you click on the delete button (found the jQuery dialog box) a Django function will do the delete job.

So how do I make the delete button (found the jQuery dialog box) send a post message (with respective variables) to a Django function found in my views.py that will do the delete job?

Real examples would be truly appreciated!

2 Answers 2

9

Say you have something like this in your template:

<div id="dialog" title="Confirm delete">Are you sure?</div>
{% for object in object_list %}
# display whatever you like here
<a id="{{ object.id }}" class="delete" href="#">Delete</a>
{% endfor %}

Then something like this (in your $(document).ready) would work -- notice how we set the callback function that the dialog calls when the delete button is pressed (using the dialog's option method) in the click handler:

$("#dialog").dialog({
    modal: true,
    autoOpen: false
});
$("a.delete").click(function(e) {
    e.preventDefault();
    var id = $(this).attr('id');
    $("#dialog").dialog('option', 'buttons', {
        "Delete": function() {
            $.post({
                url: {% url myapp.views.delete %},
                data: {'id': id},
                success: function() {
                   # whatever you like, some fancy effect that removes the object
                }
            });
            $(this).dialog("close");
        },
        "Cancel": function() {
            $(this).dialog("close");
        }
    });
    $("#dialog").dialog("open");
    return false;
});
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for the quick response. Any ideas how to add an ID to the dialog "delete" button? I have multiple forms on the same page and the way I make the distinction from what form the post request came from is by identifying the button id like this: if 'my_button_id' in request.POST: ...then do something...
2

You should also consider Aprise. It is lovely, uses jQuery, is easy to use, and is very small (3k).

apprise('Hello now?', {'verify':true});

aprise dialog

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.