Django TypeError 'method' object is not subscriptable

35,523

The problematic line tries to use subscript notation with method get of the mapping GET:

query = self.request.GET.get["q"]

The method should be called with:

query = self.request.GET.get("q")
Share:
35,523
Михаил Павлов
Author by

Михаил Павлов

Updated on August 03, 2022

Comments

  • Михаил Павлов
    Михаил Павлов almost 2 years

    I'm going through django tutorial and having the TypeError 'method' object is not subscriptable. The error is thrown when the following code executed

    class ProductListView(ListView):
        model = Product
        queryset = Product.objects.all()
    
        def get_context_data(self, *args, **kwargs):
            context = super(ProductListView, self).get_context_data(*args, **kwargs)
            context["now"] = timezone.now()
            context["query"] = self.request.GET.get["q"]
            return context
    
        def get_queryset(self, *args, **kwargs):
            print(self.request)
            qs = super(ProductListView, self).get_queryset(*args, **kwargs)
            query = self.request.GET.get["q"]
            if query:
                qs = self.model.objects.filter(
                    Q(title__icontains=query) |
                    Q(description__icontains=query)
                )
                try:
                    qs2 = self.model.objects.filter(
                        Q(price=query)
                    )
                    qs = (qs | qs2).distinct()
                except:
                    pass
            return qs
    

    The problem line is query = self.request.GET.get["q"]

    How do I solve this issue?