Python Django TypeError“方法”对象不可下标
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/36634525/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Django TypeError 'method' object is not subscriptable
提问by Михаил Павлов
I'm going through django tutorial and having the TypeError 'method' object is not subscriptable. The error is thrown when the following code executed
我正在阅读 django 教程,并且 TypeError 'method' 对象不可下标。执行以下代码时抛出错误
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"]
问题线是 query = self.request.GET.get["q"]
How do I solve this issue?
我该如何解决这个问题?
回答by Ilja Everil?
The problematic line tries to use subscript notation with method get
of the mapping GET
:
有问题的行尝试将下标符号与get
映射方法一起使用GET
:
query = self.request.GET.get["q"]
The method should be called with:
该方法应调用:
query = self.request.GET.get("q")