Невозможно изменить значение в объекте модели с помощью кнопки
Мне нужно изменить значение quantity
в item
при нажатии пользователем кнопки на странице. Мой код работает, но только один раз: если я нажимаю на кнопку в первый раз, значение меняется, но во второй или третий раз ничего не происходит
views.py
def item(request, item_id):
item = Item.objects.get(id=item_id)
context = {'item': item}
return render(request, 'main/item.html', context)
def change_quantity(request, item_id):
item = Item.objects.get(id=item_id)
context = {'item': item}
if request.method == 'GET':
item.quantity +=1
return render(request, 'main/item.html', context)
urls.py
urlpatterns = [
path('', views.index, name='index'),
path('categories/', views.categories, name='categories'),
path('categories/<category_id>/', views.category, name='category'),
path('topics/', views.topics, name='topics'),
path('categories/category/<item_id>/', views.item, name="item"),
path('categories/category/item/change_quan/<item_id>/', views.change_quantity, name='change_quan')
models.py
class Item(models.Model):
category = models.ForeignKey(Category, on_delete = models.CASCADE)
title = models.CharField(max_length = 150)
description = models.CharField(max_length = 250)
text = models.TextField()
photo1 = models.ImageField(upload_to = 'static/')
photo2 = models.ImageField(upload_to = 'static/')
small = False
quantity = 1
def __str__(self):
return self.title
item.html
{% extends "main/base.html" %}
{% load static %}
<body>
{% block content %}
<li>
<img src="{{item.photo1.url}}">
<img src="{{item.photo2.url}}">
<p> Название {{ item.title}} </p>
<p> Краткое описание {{ item.description | linebreaks }} </p>
<p> Большое описание {{ item.text | linebreaks }}</p>
<p> Количество {{ item.quantity }} </p>
<form action="{% url 'main:change_quan' item.id %}" method="get" >
{% csrf_token %}
<button type="submit" name="change_quan" > + </button>
</form>
</li>
{% endblock content %}