11

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?

0

1 Answer 1

40

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")
0

Not the answer you're looking for? Browse other questions tagged or ask your own question.