django-easy-pdf icon indicating copy to clipboard operation
django-easy-pdf copied to clipboard

TypeError at /cart/ get_context_data() missing 1 required positional argument: 'request'

Open AmanSwarnkar08 opened this issue 6 years ago • 2 comments

class ViewCart(TemplateView): template_name = 'cart/view_cart.html'

def get_context_data(self,request,**kwargs):
    context = super().get_context_data(request,**kwargs)
    try:
        the_id = request.session['cart_id']
    except:
        the_id = None
    if the_id:
        cart = Cart.objects.get(id = the_id)
        context['cart'] = cart
        context['empty'] = False
    else:
        empty_message = "Your Cart is Sad!, please keep shopping."
        context['empty'] = True
        context['empty_message'] = empty_message

    return context

AmanSwarnkar08 avatar Oct 24 '19 09:10 AmanSwarnkar08

Try this it might be of help to you:

class ViewCart(TemplateView):
    template_name = 'cart/view_cart.html'
    def get_context_data(self,**kwargs):
        context = super(ViewCart, self).get_context_data()
        try:
            the_id = self.kwargs['cart_id']
        except:
            the_id = None
        if the_id:
            cart = Cart.objects.get(id = the_id)
            context['cart'] = cart
            context['empty'] = False
        else:
            empty_message = "Your Cart is Sad!, please keep shopping."
            context['empty'] = True
            context['empty_message'] = empty_message
        return context

mjombanorman avatar Jul 28 '21 18:07 mjombanorman

get_context_data is in your class view which already has "request", so you don't need to wait for it in this method you can call it by self.request

def get_context_data(self, **kwargs):
    context = super().get_context_data(**kwargs)
    try:
        the_id = self.request.session['cart_id']

andrey-backend-dev avatar Sep 24 '21 19:09 andrey-backend-dev