Reputation: 2393
I am trying to do a simple POST request via AJAX, but I keep getting 500 errors. In an effort to figure out where the problem is, I added print statements after every if conditional. See my view below.
if request.is_ajax():
print "request is ajax"
object_name = request.POST.get('entryname')
print object_name
targetobject = Entry.objects.get(headline=object_name)
if request.user.username == targetobject.author:
print "current user is author"
targetobject.delete()
return HttpResponseRedirect('/storefront/')
The only thing that prints to the console (other than the 500 error) is "request is ajax", and "None". This tells me that something is wrong with 'object_name' or 'entryname'. Below is the jQuery AJAX.
<script type="text/javascript">
var my_app = {
username: "{{ request.user.username }}"
};
</script>
<script>
$(document).ready(function() {
$(".delete_button").click(function() {
var entryname = $(this).attr('id');
$.ajax({
type: "POST",
url: "/accounts/" + my_app.username + "/listview/",
data: {'entryname': entryname},
csrfmiddlewaretoken: '{{ csrf_token }}'
});
return false;
});
});
</script>
<a href="" class="delete_button" id="{{ i.headline }}">del</a>
And, this error in the console
DoesNotExist: Entry matching query does not exist.
Where did I go wrong? Is the error in the jQuery or my view method?
Upvotes: 0
Views: 151
Reputation: 3755
Your code looks like it's looking for the id
of the element with class delete_button
. Does your template contain an element with that class? If so, what is its id
? That is the value that should be passed as entryname
, but if there's no such element in your template then it would make sense that a value of None
is being passed (as you mentioned in your comments). Try adding the delete_button
class to your link tag.
Upvotes: 1