Skip to content

Instantly share code, notes, and snippets.

@cantoniazzi
Created July 19, 2017 21:51
Show Gist options
  • Save cantoniazzi/9d4428716208e0bd66d6b9fab4c58828 to your computer and use it in GitHub Desktop.
Save cantoniazzi/9d4428716208e0bd66d6b9fab4c58828 to your computer and use it in GitHub Desktop.
template
$(document).on("click", ".btn-remove", function() {
var id = $(this).attr("data-id");
var uri = '/sua-rota-de-remocao-de-registros';
$.ajax({
type: "POST",
url: uri,
success: function (data) {
// aqui entra sua lógica de ocultar o elemento removido
}
});
});
<table>
{% for registro in registros %}
<tr>
<td>
{{ registro.id }}
</td>
<td>
{{ registro.nome }}
</td>
<td>
<button class="btn btn-remove" data-id="{{ registro.id }}">Delete</button>
</td>
</tr>
</table>
from django.http import JsonResponse
def delete(request, id):
data = {'success': 0}
if request.method == 'POST':
try:
registro = Registro.objects.get(id=id)
except ObjectDoesNotExist:
registro = None
if registro:
registro.delete()
data['success'] = 1
return JsonResponse(data)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment