I have a general view of Django (ListView), and I would like to add options for organizing objects by its methods, which are a bit complicated and perhaps cumbersome to implement with something like extra(). I understand that the only way to do this is to sort () the Queryset something like this:
sorted(queryset, key=lambda q: q.choice_entropy())
However, to include this in the ListView, as well as allow users to select ordering criteria, I would require something like this
class IndexView(ListView):
model = Question
def get_queryset(self):
queryset = super(IndexView, self).get_queryset()
if 'order_by' in self.request.GET:
if self.request.GET['order_by'] == 'entropy':
return sorted(queryset, key=lambda q: q.choice_entropy())
elif self.request.GET['order_by'] == 'newest':
return queryset.order_by('-published_time')
elif self.request.GET['order_by'] == ... (some other sorting criterion)
return queryset
The problem here (presumably) is that it sorted(queryset, key=lambda q: q.choice_entropy())returns a <type 'list'>why it querysethas a type <class 'django.db.models.query.QuerySet'>.
, , self.queryset , . ListView, , , A server error occurred. Please contact the administrator. :
Internal Server Error: /
Traceback (most recent call last):
File "/Users/zazapachulia/.virtualenvs/qandapp-dev/lib/python2.7/site-packages/django/core/handlers/base.py", line 140, in get_response
response = response.render()
File "/Users/zazapachulia/.virtualenvs/qandapp-dev/lib/python2.7/site-packages/django/template/response.py", line 105, in render
self.content = self.rendered_content
File "/Users/zazapachulia/.virtualenvs/qandapp-dev/lib/python2.7/site-packages/django/template/response.py", line 80, in rendered_content
template = self.resolve_template(self.template_name)
File "/Users/zazapachulia/.virtualenvs/qandapp-dev/lib/python2.7/site-packages/django/template/response.py", line 56, in resolve_template
return loader.select_template(template)
File "/Users/zazapachulia/.virtualenvs/qandapp-dev/lib/python2.7/site-packages/django/template/loader.py", line 184, in select_template
raise TemplateDoesNotExist("No template names provided")
TemplateDoesNotExist: No template names provided
Traceback (most recent call last):
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/wsgiref/handlers.py", line 85, in run
self.result = application(self.environ, self.start_response)
File "/Users/zazapachulia/.virtualenvs/qandapp-dev/lib/python2.7/site-packages/django/contrib/staticfiles/handlers.py", line 72, in __call__
return self.application(environ, start_response)
File "/Users/zazapachulia/.virtualenvs/qandapp-dev/lib/python2.7/site-packages/django/core/handlers/wsgi.py", line 255, in __call__
response = self.get_response(request)
File "/Users/zazapachulia/.virtualenvs/qandapp-dev/lib/python2.7/site-packages/django/core/handlers/base.py", line 178, in get_response
response = self.handle_uncaught_exception(request, resolver, sys.exc_info())
File "/Users/zazapachulia/.virtualenvs/qandapp-dev/lib/python2.7/site-packages/django/core/handlers/base.py", line 217, in handle_uncaught_exception
return debug.technical_500_response(request, *exc_info)
File "/Users/zazapachulia/.virtualenvs/qandapp-dev/lib/python2.7/site-packages/django/views/debug.py", line 69, in technical_500_response
html = reporter.get_traceback_html()
File "/Users/zazapachulia/.virtualenvs/qandapp-dev/lib/python2.7/site-packages/django/views/debug.py", line 297, in get_traceback_html
c = Context(self.get_traceback_data())
File "/Users/zazapachulia/.virtualenvs/qandapp-dev/lib/python2.7/site-packages/django/views/debug.py", line 237, in get_traceback_data
for loader in template_source_loaders:
TypeError: 'NoneType' object is not iterable
, NoneType , manage.py Python, , .
- . , . ?
list queryset, .
, , , , , , , , , .