Django TypeError 'method' object is not subscriptable

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?

Upvotes: 12

Views: 42522

Answers (1)

Ilja Everilä
Ilja Everilä

Reputation: 52937

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

Upvotes: 41

Related Questions