WayBehind
WayBehind

Reputation: 1697

Django Pagination KeyError 'source'

I installed Django template pagination tag from: https://github.com/jmcclell/django-bootstrap-pagination and while I followed all the instructions, I'm getting 'source' errors.

Apparently I'm doing something wrong.

===========================================

EDIT 3

SETTINGS.py

TEMPLATE_CONTEXT_PROCESSORS = (
"django.contrib.auth.context_processors.auth",
"django.core.context_processors.request",
)

VIEW.py:

def pagination(request):
location = Location.objects.all()
return render_to_response('pagination.html',
                      location,
                      context_instance=RequestContext(request))

TEMPLATE

{% load bootstrap_pagination %}

<h1>Location</h1>
{% for location in location %}
<h2>{{ location.name }}</h2>
{% endfor %}

{% bootstrap_paginate location %}

ERROR:

AttributeError at /pagination/

'str' object has no attribute 'paginator'

Request Method: GET
Request URL:    http://127.0.0.1:8000/pagination/
Django Version: 1.5.4
Exception Type: AttributeError
Exception Value:    
'str' object has no attribute 'paginator'

Error during template rendering

In template /home/jr/Documents/python/amapp/sdr/article/templates/pagination.html, error     at line 7
'str' object has no attribute 'paginator'
1   {% load bootstrap_pagination %}
2   
3   
4   
5   
6   
7   {% bootstrap_paginate location %}

Upvotes: 0

Views: 2023

Answers (3)

Jules
Jules

Reputation: 1747

Using page_obj worked for me, for anyone wondering why this doesn't work when using Django 2.0.2 and django-bootstrap4 0.0.6.

I came across this by digging through the context variables in the error message that showed up from Django's DEBUG mode.

Upvotes: 0

Jason McClellan
Jason McClellan

Reputation: 3021

I'm the author of the library, but you must forgive me as I haven't used Python in quite some time and I've core dumped most of my knowledge about this library.

So, @WayBehind was correct, your first mistake was using "page_obj". That was simply an example. In your case, you want to use "location"

However, you never got to see that error because you have a more pressing error which is that the library isn't playing nice with your setup. I wrote this library with Python 2.7 with the request context preprocessor. Please double check that you have the context preprocessor enabled as per the documentation and please be sure you are using Python <3.0. I know for a fact the library does not currently working on 3.x. There is a fork of the library where some other folks have been working to fix that and I am actively keeping an eye on it to pull those changes in when ready, but as of now it just doesn't work.

If you are using Python 2.x and you have the request context preprocessor enabled, I am not sure why you would be getting that error. If you can confirm those two things are true, I'll be happy to take a closer look tomorrow.

Edit:

This may or may not be an issue, but I notice that you loop through your Location object using the same variable name for the instance:

{% for location in location %}
<h2>{{ location.name }}</h2>
{% endfor %}

{% bootstrap_paginate location %}

Is it possible that Django's template scoping is such that the object you are passing to bootstra_paginate is the last instance of "location" rather than the entire set? This is an off the cuff guess as a first stab at this because otherwise things appear to be correct.

Upvotes: 2

AlvaroAV
AlvaroAV

Reputation: 10553

Have you followed all the steps ?

Request is in the context_processor (settings.py)?

TEMPLATE_CONTEXT_PROCESSORS = (
    ....
    "django.core.context_processors.request",
    ....
)

You are using obj_list in the template, but do you have anything inside obj_list ? Maybe you have to use "location" instead of "obj_list" ? Because I think your object list is inside location (Location objects) but you are using obj_list like in the example. In the example obj_list is just a variable example for an object list.

EDIT: Change this:

def pagination(request):
    args = {}
    args.update(csrf(request))
    args['location'] = Location.objects.all()
    return render_to_response('pagination.html', args)

for this:

from django.template import RequestContext
    def pagination(request):    
        location = Location.objects.all()
        return render_to_response('pagination.html', 'location':location,context_instance=RequestContext(request))

Upvotes: 0

Related Questions