Essentially, I'm trying to eliminate the need to show the id of a 'listing' to the user. I'm using Hashids to encode the automatically created id as a unique code.
To show a single listing, I've been currently doing this:
@app.route('/listing/<uniqueHash>')
def listing(uniqueHash):
a = hashids.decode(uniqueHash)
listing = models.Listing.select().where( models.Listing.id == a ).get()
uniqueHash = hashids.encode(listing.id)
return render_template("test1.html", listing = listing, uniqueHash = uniqueHash)
and this works fine for a single listing. However, if I wanted to display multiple listings like this:
@app.route('/')
def index():
listings = models.Listing.select().limit(100)
return render_template("test.html", listings = listings)
I then can't provide the unique id for every listing to the jinja2 template (and don't see a way to encode/decode ids in the jinja template itself...is there?)
I'd ultimately like to store the unique code in the database, however am confused as to how to create the hashid - based off the id of the listing - when I'm creating that listing itself and don't know the id.
I'm currently creating a listing like this:
form = request.form
models.Listing.create(
title = form['title'],
description = form['description'],
price = form['price']
)
I'm planning later to use this with WTForms for validation.
How am I best to include the unique code in the creation of a listing without knowing the id itself???
Any help is greatly appreciated!!!