Делаю так
Модель
class Book(models.Model):
class Meta:
db_table = "Book"
book_desk = models.ForeignKey(Desk, on_delete=models.DO_NOTHING, verbose_name="Товар")
book_user = models.ForeignKey(User, on_delete=models.DO_NOTHING, verbose_name="Пользователь")
шаблон
{% extends 'base.html' %}
{% load static %}
{% block desk %}
{% for desk in desks %}
<div class="col-lg-9 col-lg-offset-1">
<div class="row" style="margin-top: 30px;">
<a href="{% url 'desk' desk.id %}">
<div class="col-lg-2">
{% if desk.desk_image_main %}
<img src="{{ desk.desk_image_main.url }}" width="170px" height="170px">
{% endif %}
</div>
</a>
<div class="col-lg-10 desk_all" style="text-align: left;">
<div class="tab-pane row">
<div class="col-lg-3" style="text-align: left;">
<h3 style="text-align: left;">
{{ desk.desk_name }}
</h3>
</div>
<div class="col-lg-3">
<h6 style="font-style: italic; float: right; margin-top: -20px; opacity: .5;">
дата:{{ desk.desk_date }}
</h6>
<h4 style="font-style: italic; float: right;">
<img src="{% static 'media/images/money.png' %}" width="20%;">
{{ desk.desk_price }} ₽
</h4>
{% if desk.id in books_list %}
<div data-type="desk" data-action="book" title="Избранное">
<h4 style="font-style: italic; float: right; opacity: .5;">
<span class="glyphicon glyphicon-heart-empty"></span>
</h4>
</div>
{% else %}
<div data-type="desk" data-action="book" title="Избранное">
<h4 style="font-style: italic; float: right; color: crimson;">
<span class="glyphicon glyphicon-heart"></span>
</h4>
</div>
{% endif %}
<div data-id="{{ desk.id }}" data-type="desk" data-action="book" title="Избранное">
<span class="glyphicon glyphicon-star"></span>
</div>
</div>
</div>
</div>
</div>
</div>
{% endfor %}
<script>
function to_book()
{
var current = $(this);
var id = current.data('id');
var action = current.data('action');
$.ajax({
url : "/desks/" + get + "/" + id + "/" + action + "/",
type : 'POST',
data : { 'book' : id },
success : function (json) {
current.find("[data-count='" + action + "']").text(json.count);
}
});
return false;
}
// Подключение обработчика
$(function() {
$('[data-action="book"]').click(to_book);
});
</script>
<script>
// Получение переменной cookie по имени
function getCookie(name) {
var cookieValue = null;
if (document.cookie && document.cookie !== '') {
var cookies = document.cookie.split(';');
for (var i = 0; i < cookies.length; i++) {
var cookie = jQuery.trim(cookies[i]);
// Does this cookie string begin with the name we want?
if (cookie.substring(0, name.length + 1) === (name + '=')) {
cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
break;
}
}
}
return cookieValue;
}
// Настройка AJAX
$(function () {
$.ajaxSetup({
headers: { "X-CSRFToken": getCookie("csrftoken") }
});
});
</script>
{% endblock %}
@login_required()
def book(request, desk_id):
desk = Desk.objects.get(id=desk_id)
book_new, created = Book.objects.get_or_create(book_user_id=request.user.id, book_desk_id=desk.id)
if not created:
book_new.delete()
return HttpResponse(
json.dumps({
"result": created,
"count": Book.objects.filter(book_desk_id=desk.id).count()
}),
content_type="application/json"
)
url(r'^desks/get/(?P<desk_id>\d+)/book/$', views.book, name='book'),
первый день с ajax пытаюсь работать очень интересна эта технология но картинка не складывается помогите пожалуйста