how do I solve Django TypeError - post() got multiple values for argument 'slug'?

Viewed 919

I am creating a e commerce website and I am a beginner in Django and Python

My cart name is "Kart"

The cart is an cart object in session. It store the cart in session. it Works in main shop page but it is not working in product_details(productView)

The views.py logic adds the product in cart but it dose not redirect to product page as a post request. I have to click on address bar and reload again to get back to product page.

Product details page

Views.py

class productView(View):

def post(request, slug ):

    product = request.POST.get('product')
    remove = request.POST.get('remove')
    kart = request.session.get('kart')
    if kart:
        quantity = kart.get(product)
        if quantity:
            if remove:
                if quantity <=1:
                    kart.pop(product)
                else:
                    kart[product] = quantity-1
            else:
                kart[product] = quantity+1
        else:
            kart[product] = 1
    else:
        kart = {}
        kart[product] = 1

        
    request.session['kart'] = kart
    return redirect('product-view', slug=slug)



def get(self,request, slug):
    product = Product.objects.filter(product_slug=slug)
    return render(request, 'T_kart/ProductView.html', {'product': product[0]})

urls.py

from django.contrib import admin
from django.urls import path
from . import views

app_name = 'T_kart'
urlpatterns = [
    path('', views.index.as_view(), name="T-kart-Home"),
    path('product-<slug:slug>/', views.productView.as_view(), name="product-view"),
    path('kart/', views.kart.as_view(), name="kart"),
    path('checkout/', views.checkout, name="checkout"),
    path('search/', views.search, name="search"),

    path('wishlist/', views.wishlist, name="wishlist"),


]

Template (html)

<div style="display: flex; align-items: center;  justify-content: center; margin-top: 30px ">
          <form action="/t-kart/product-{{product.product_slug}}/" method="post">
            {% csrf_token %}
            <input type="text" hidden name="product" value="{{product.id}}">
            <input type="text" hidden name="remove" value="True">
            <input id='minus' type="submit"
              class="u-btn-1 u-button-style u-custom-color-1 u-text-hover-custom-color-1 minus"
              style="padding: 10px; width: 44px; margin: 0;" value="-"></input>
          </form>
          <span id='val' class='u-custom-font' style="margin: 7px;">{{ product|cart_product_qty:request.session.kart}} in kart</span>
          <form action="/t-kart/product-{{product.product_slug}}/" method="post">
            {% csrf_token %}
            <input type="text" hidden name="product" value="{{product.id}}">
            <input id='plus' type="submit"
              class=' u-btn-1 u-button-style u-custom-color-1 u-text-hover-custom-color-1 plus'
              style="padding: 10px; width: 44px; margin: 0;" value="+"></input>
          </form>
        </div>

     <form action="/t-kart/product-{{product.product_slug}}/" method="POST">
          {% csrf_token %}
          <input type="text" hidden name="product" value="{{product.id}}">

          <input type="submit" class="
            u-align-center-sm
            u-align-center-xs
            u-btn
            u-button-style
            u-custom-color-1
            u-hover-custom-color-2
            u-text-custom-color-3
            u-text-hover-custom-color-1
            u-btn-1
          " value="Add to Kart"></input>
        </form>

Traceback

Traceback (most recent call last):
  File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/django/core/handlers/exception.py", line 47, in inner
    response = get_response(request)
  File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/django/core/handlers/base.py", line 181, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/django/views/generic/base.py", line 70, in view
    return self.dispatch(request, *args, **kwargs)
  File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/django/views/generic/base.py", line 98, in dispatch
    return handler(request, *args, **kwargs)

Exception Type: TypeError at /t-kart/product-APPLE-iPhone-11-Black-64-GB/

Exception Value: post() got multiple values for argument 'slug'
1 Answers

Your post method should start with a self:

def post(self, request, slug):
    # …
    pass

If you do not do this, then request will take as value the object, slug will take the request, and there will be an extra parameter in the call: slug=value-for-slug, hence like the error says, your slug parameter will have two values, which makes not much sense.

Related