Я разрешаю пользователям удалять сообщения через ajax. Сообщения имеют логическое поле live_until_removed. Если установлено значение false, сообщение исчезает.Django Ajax форма представления, указывающая на 403 Forbidden
При нажатии удалить я дан 403, ссылки:
xhr.send((s.hasContent && s.data) || null);
Как я могу получить это, чтобы работать бесперебойно? Почему эта ошибка происходит?
ЯШ:
$('#removeForm').submit(function() { // catch the form's submit event
$.ajax({
data: $(this).serialize(),
type: $(this).attr('method'),
url: $(this).attr('action'),
success: function(response) {
$('.close-post').html(response); // update the DIV
console.log(response);
},
error: function(response){
console.log(response);
}
});
return false;
});
шаблона:
<div class="close-post">
{% if not post.live_until_removed %}
<form class="" id="removeForm" method="POST" action="">
<button type="submit" class="btn">Remove</button>
</form>
{% else %}
<button class="btn">Removed</button>
{% endif %}
</div>
views.py:
def post(request, id):
...
if request.is_ajax():
try:
post = Post.objects.get(id=id)
post.live_until_removed = False
post.save()
response = simplejson.dumps({"status": "Removed"})
except:
pass
Посмотрите здесь, как вставлять маркер CSRF в каждом запросе AJAX: http://stackoverflow.com/questions/6506897/csrf-token-missing-or-incorrect-while-post-parameter-via -ajax-in-django/6533544 # 6533544 – Mounir