我想返回一个排序列表,当request method = GET时,搜索框提供给定的查询字符串(如果它不为null)。我可以在这个CBV中做到这一点,还是我必须为这个工作定义一个新视图?
我的views.py:
class DolgozokListView(ListView):
model = Dolgozo
def get_context_data(self, **kwargs):
context = super(DolgozokListView, self).get_context_data(**kwargs)
return context
def get(self, request):
search_query = request.GET.get('search_box', None)
if (search_query is not None):
context = super(DolgozokListView, self).object_list.filter(nev__conatains = search_query)
else:
context = super(DolgozokListView, self).get_context_data
return context
但我收到以下错误:
Environment:
Request Method: GET
Request URL: http://localhost:50033/dolgozo/
Django Version: 1.9.1
Python Version: 2.7.11
Installed Applications:
('django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
'django.contrib.messages',
'django.contrib.staticfiles',
'app',
'DolgozoKarbantart',
'DolgozoMunkakorKarbantart',
'FelhasznaloKarbantart',
'MunkaIdoAdatKarbantart',
'MunkakorKarbantart')
Installed Middleware:
('django.middleware.common.CommonMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware')
Traceback:
File "C:\Python27\lib\site-packages\django\core\handlers\base.py" in get_response 235. response = middleware_method(request, response)
File "C:\Python27\lib\site-packages\django\middleware\common.py" in process_response
112. if response.status_code == 404:
Exception Type: AttributeError at /dolgozo/
Exception Value: 'function' object has no attribute 'status_code'
答案 0 :(得分:0)
def get_queryset(self):
qs = self.model.objects.all()
search = self.request.GET.get('search_box', None)
if search:
qs=qs.filter(nev__icontains=search)
return qs