T
T
Tryggvi2018-05-21 21:55:45
Django
Tryggvi, 2018-05-21 21:55:45

How to do an add to cart from a shared directory?

Hello.
There is an addition of a product in a detailed review of the product:
#views.py

def ProductDetail(request, id, slug):
    product = get_object_or_404(Product, id=id, slug=slug, available=True)
    cart_product_form = CartAddProductForm()
    return render_to_response('catalog/allCatalog/BaseProduct.html',
                             {'product': product,
                              'cart_product_form': cart_product_form})

Sample
<form action="{% url 'cart:CartAdd' product.id %}" method="post">
            <label class="cart_quantity" for="id_quantity">Количество:</label>
            {% csrf_token %}
            {{ cart_product_form }}
            
            <input class="product_details_basket vcenter text-center" type="submit" value="Добавить в корзину">
          </form>

In the general product catalog, they are rendered like this:
class catalog_infrared(ListView):
    model = Product
    template_name = 'catalog/product_list.html'
    context_object_name = 'product'  # Default: object_list
    paginate_by = 9
    queryset = Product.objects.all().order_by('price').filter(category__name='Теплый пол', categorysub__name='пленочный')

class catalog_mats_devi(ListView):
    model = Product
    template_name ='catalog/product_list.html'
    context_object_name = 'product'  # Default: object_list
    paginate_by = 9
    queryset = Product.objects.all().order_by('price').filter(category__name='Теплый пол', categorysub__name='нагревательный мат', brand__name='Devi')

But if I substitute the same form as in the detailed review of the product in the general catalog, then adding to the cart does not occur. It just goes to the cart.
[21/May/2018 21:04:17] "POST /cart/add/38/ HTTP/1.1" 302 0
[21/May/2018 21:04:17] "GET /cart/ HTTP/1.1" 200 4349

As I understand it, you need to pass the id to the ListView, or can you get it from the URL, or how can this be implemented?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexey Cheremisin, 2018-05-21
@Tryggvi

Most likely, and even better, add to the cart not through the form, but through ajax! In other words - via javascript using the POST or PUT method with the transfer in the JSON body

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question