Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
AmanSwarnkar08 opened this issue Oct 24, 2019 · 2 comments

Comments

@AmanSwarnkar08
Copy link

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
@mjombanorman
Copy link

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

@andrey-backend-dev
Copy link

andrey-backend-dev commented Sep 24, 2021

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']

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants