I do online store on django. I do not understand how to implement the add button in the basket. Let's say there is a product model. And the model of the basket where the ordered goods are stored. The page displays phone models in the following way:

<div class="modelList"> {% for model in model_list %} <div class="model"> <div class="modelImage"> <img src="{% static 'images/model.jpg' %}"> </div> <div class="discription"> <div class="modelName">{{ model.name}}</div> <div class="modelDiscription"> {{ model.description}} </div> </div> <div class="actions"> <div class="colors"> </div> <div class="price">{{ model.price }} р.</div> <button>Добавить в корзину</button> </div> </div> {% endfor %} </div> 

How to make the button when you send sent the goods to Corinna?

    1 answer 1

    First, I recommend getting rid of the basket model in favor of the purchase model:

     class Purchase(models.Model): customer = models.ForeignKey(User) # Кто купил item = models.ForeignKey(Goods) # Что купил count = models.PositiveIntegerField() # Сколько купил ... # И другие нужные поля. Например, когда купил. 

    This model is easier to expand in the future, it is easier to build reports on it, etc.

    Secondly, in the template, do something like <a href="{% url 'basket:add' model.pk %}">Добавить в корзину</a>

    Finally in view:

     def add_to_backet(request, item_id): item = Goods.objects.get(pk=item_id) Purchase.objects.create(customer=request.user, item=item, count=1) 

    This is just a skeleton for an example, it needs to be improved!