In Django 1.4.2, I use a simple pagination code like given in official documentation:
...
paginator = Paginator(songs, 25) # Show 25 songs per page
page = request.GET.get('page')
try:
songs = paginator.page(page)
except PageNotAnInteger:
# If page is not an integer, deliver first page.
songs = paginator.page(1)
except EmptyPage:
# If page is out of range (e.g. 9999), deliver last page of results.
songs = paginator.page(paginator.num_pages)
...
When I run that view, I get "'Manager' object is unsubscriptable" error on songs = paginator.page(1)
line. I searched error but I couldn't find any solution about that. What is the problem?
EDIT:
songs
is django model object list. Full view code is like that:
def index(request):
songs = Song.objects
#filter params
q_name = request.GET.get('name', None)
if q_name:
songs = songs.filter(name__contains=q_name)
q_composer = request.GET.get('composer', None)
if q_composer:
songs = songs.filter(composer__name__contains=q_composer)
q_composer_id = request.GET.get('composer_id', '')
if q_composer_id != '':
songs = songs.filter(composer__id=q_composer_id)
paginator = Paginator(songs, 25) # Show 25 contacts per page
page = request.GET.get('page')
try:
songs = paginator.page(page)
except PageNotAnInteger:
# If page is not an integer, deliver first page.
songs = paginator.page(1)
except EmptyPage:
# If page is out of range (e.g. 9999), deliver last page of results.
songs = paginator.page(paginator.num_pages)
return render(request, 'index.html', {'songs': songs})
What happens when there are no filters provided. Then you are trying to paginate a AuditManger and not the queryset. You should change the first query to this:
songs = Song.objects.all()