python - UndefinedError: 'url_for_other_page' is undefined -


i encountering (probably) rookie mistake inherited code within flask. helper function inside template, url_for_other_page() not being initialized when page in question loads. returns self-descriptive error:

undefinederror: 'url_for_other_page' undefined

i know what's causing -- helper method not getting polled because it's not exposed url in question, or something. what's best approach without exposing more necessary url path in question? should helper function elsewhere in application logic (such in init?)

below template snippet causes offending error, , entire core.py, application logic lives. requests made /images, returns results of entries_index() template call. pagination code pretty co-opted flask's own examples. full source can viewed on github if need additional context.

core.py

import hashlib, os   flask import request, session, render_template, flash, url_for, \         redirect, send_from_directory werkzeug import secure_filename kremlin import app, db, dbmodel, forms, imgutils, uploaded_images pagination import pagination  @app.route('/') def home_index():     """ display glasnost logo, attempt replicate old behavior """     return render_template('home.html')  @app.route('/images', defaults={'page': 1}) @app.route('/images/page/<int:page>') def entries_index(page):     """ show index of image thumbnails """     posts = dbmodel.post.query.all()     pagination = pagination(page, 2, len(posts))     #import pdb; pdb.set_trace()     return render_template('board.html', form=forms.newpostform(),         posts=posts, pagination=pagination)  # offending helper method def url_for_other_page(page):     args = request.view_args.copy()     args['page'] = page     return url_for(request.endpoint, **args)     app.jinja_env.globals['url_for_other_page'] = url_for_other_page  @app.route('/images/<int:post_id>') def view_post(post_id):     """ show post identified post_id """     post = dbmodel.post.query.get_or_404(post_id)     comments = dbmodel.comment.query.filter_by(parent_post_id=post_id)     return render_template('post.html', post=post, comments=comments)  @app.route('/images/get/<filename>') def send_file(filename):     """send image file browser"""     return send_from_directory(app.config['uploaded_images_dest'],         filename)  @app.route('/images/add/', methods=['post']) def add_image():     """ add new image """      form = forms.newpostform()      if form.validate_on_submit():         filename = secure_filename(form.upload.file.filename)         fileext = os.path.splitext(filename)[1]         filedata = form.upload.file.stream.read()          # calculate sha1 checksum         h = hashlib.new('sha1')         h.update(filedata)         filehash = h.hexdigest()          # validate file uniqueness         dupe = dbmodel.image.query.filter_by(sha1sum=filehash).first()          if dupe:             flash("image exists: %s" % (dupe))             return redirect(url_for('entries_index'))         else:             # file unique, proceed create post , image.             # save file filesystem              # rewind file, read() sha1 checksum             # routine             form.upload.file.seek(0)              # proceed storage             try:                 uploaded_images.save(storage=form.upload.file,                                      name=''.join([filehash, '.']),                                     )                  # fixme: generate thumbnail in safer way.                 # horrible , i'm sorry.                 imagepath = uploaded_images.path(''.join([filehash, fileext]))                 imgutils.mkthumb(imagepath)             except ioerror:                 flash("oh god terrible error occured while saving %s" %                     (filename))             else:                 dbimage = dbmodel.image(filename, filehash)                 db.session.add(dbimage)                  user = none                  if "uid" in session:                     user = dbmodel.user.query.filter_by(                             id=session['uid']                         ).first()                  note = form.note.data                  #todo: implement tags.                  # create new post image                 post = dbmodel.post(image=dbimage, title=filename,\                         note=note, user=user)                 db.session.add(post)                  # commit database transaction                 db.session.commit()                 flash("image posted!")          return redirect(url_for('entries_index'))     else:         flash("your form has terrible errors in it.")         return(redirect(url_for("entries_index"))) 

template call (excerpt board.html)

<div id='imageboardpagetop'>       <div class=pagination>       {% page in pagination.iter_pages() %}         {% if page %}           {% if page != pagination.page %}             <a href="{{ url_for_other_page(page) }}">{{ page }}</a>           {% else %}             <strong>{{ page }}</strong>           {% endif %}         {% else %}           <span class=ellipsis>…</span>         {% endif %}       {% endfor %}       {% if pagination.has_next %}         <a href="{{ url_for_other_page(pagination.page + 1)              }}">next &raquo;</a>       {% endif %}       </div> </div> 

this line needs go core.py , not inside function - url_for_other_page -

app.jinja_env.globals['url_for_other_page'] = url_for_other_page 

code -

def url_for_other_page(page):     args = request.view_args.copy()     args['page'] = page     return url_for(request.endpoint, **args) app.jinja_env.globals['url_for_other_page'] = url_for_other_page 

Comments

Popular posts from this blog

toolbar - How to add link to user registration inside toobar in admin joomla 3 custom component -

linux - disk space limitation when creating war file -