Reputation: 423
I'm working on a project in Flask, where we need to add a Pagination class to load results in. I ended up using a predefined method outlined here and i'm running into a NameError when I attempt to load the Images index:
NameError: global name 'Pagination' is not defined
I know this has to be something simple i've overlooked, such as where i'm declaring the Pagination object, but if anyone has any insight on what that is, I'd appreciate it. Being new to Python has its downsides.
Here is the code for the Route call in core.py (including the failing NameError call on line 6)
#This import call initalizes Pagination as part of forms. There are other calls that
#are not pertinent
from kremlin import app, db, dbmodel, forms, imgutils, uploaded_images
#The route call for the images page
@app.route('/images', defaults={'page': 1})
@app.route('/images/page/<int:page>')
def entries_index(page):
""" Show an index of image thumbnails """
posts = dbmodel.Post.query.all()
pagination = Pagination(page, 20, posts) #This is where the error occurs
return render_template('board.html', form=forms.NewPostForm(),
posts=posts, pagination=pagination)
And the pagination object which extends forms.py
class Pagination(object):
def __init__(self, page, per_page, total_count):
self.page = page
self.per_page = per_page
self.total_count = total_count
@property
def pages(self):
return int(ceil(self.total_count / float(self.per_page)))
@property
def has_prev(self):
return self.page > 1
@property
def has_next(self):
return self.page < self.pages
def iter_pages(self, left_edge=2, left_current=2, right_current=5, right_edge=2):
last = 0;
for num in xrange(1, self.pages + 1):
if num <= left_edge or \
(num > self.page - left_current - 1 and \
num < self.page + right_current) or \
num > self.pages - right_edge:
if last + 1 != num:
yield None
yield num
last = num
I've thrown together the full source here: https://github.com/glasnost/kremlin/tree/pagination
Upvotes: 1
Views: 1180
Reputation: 1371
The NameError means that the name you're using does not exist in the current namespace. It's defined in forms.py. So, you either need to use a qualified name, or import that name into the namespace.
As Sean hinted at, to use the qualified name, this code:
pagination = Pagination(page, 20, posts)
should look like:
pagination = forms.Pagination(page, 20, posts)
Or, to make that line work as is, you should import the name into the current namespace like this:
from kremlin import app, db, dbmodel, forms, imgutils, uploaded_images
from kremlin.forms import Pagination
Upvotes: 1