B
B
bituke2020-10-14 17:07:30
Django
bituke, 2020-10-14 17:07:30

How to add products to "favorites" in django?

I was very tired with this task, I created a small test project especially for the question:
5f8703df77397764113746.png
The logic is simple: there is an application for goods, there is an application for favorite products that the user will add on their own.

there is a page with products (created in the database through models and displayed through a template engine):
5f870346b6439697217378.png

application model:

from django.db import models

class Product(models.Model):
  '''таблица с товарами, имеет только столбик названий товаров'''

  name_product = models.CharField('название товара', max_length=50)

  def __str__(self):
    return self.name_product


application view:

from django.shortcuts import render
from .models import Product

def all_product(request):
  '''Функция, которая выводит все товары которые у нас имеются'''

  all_product = Product.objects.all

  context = {
    'all_product': all_product,
  }

  return render(request, 'products/all_products.html', context)


and the application template:

<h1>Товары в наличии:</h1>
  {% for i in all_product %}
    <p>{{ i }} <a href="/">добавить</a></p>
  {% endfor %}
  <a href="/favorite_products/">Перейти в любимые товары</a>


It is required to write a function that, when clicking on the "add" button, will add the desired product to the "favorite products" page, which is currently empty.
5f8703a214cda241140915.png

I beg your help, I tried many different methods - all absolutely cast and non-working.
Ideally, when you click on "add", the product is added to the application's favorite_products database, and then the table with the added products is displayed on the previously mentioned page.
Thank you very much in advance.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
I
IDzone-x, 2020-10-16
@bituke

Isn't it possible to do just that.
Let's say we have a model called <name>
and there we create two fields, post and user which the ego saved and both of them are the primary key. then we make the form, I think you can do it yourself :) and wow.

D
Dr. Bacon, 2020-10-14
@bacon

And what, you can’t create a Favorite model, where there will be a link to the Product?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question