8. The resolve_alias Function
- Now that we have a table of URIs and an associated slug, we need to create a resolver function in
default.py
.
- Line 18 is the function definition.
- Line 19 a variable
my_slug
is populated with the first argument request.args(0)
that is passed to the function from the URL string. For example: http://milesm.pythonanywhere.com/aliases/default/resolve_alias/cnn
would pass cnn
as arg(0)
. - Line 20 populates a
rows
object with a value from a query using DAL syntax. db.the_aliaser
references the table called the_aliaser
the_slug == my_slug
means to select the row from the_aliaser
table with a value equal to the variable in my_slug
select(db.the_aliaser.the_url)
means to only return the value found in the the_url
.
- Line 21 returns all of the variables in the function to the view called
resolve_alias
.
##### Copy the code below and paste it into default.py #####
def resolve_alias():
my_slug = request.args(0)
rows = db(db.the_aliaser.the_slug == my_slug).select(db.the_aliaser.the_url)
return locals()
- DAL query language examples
- More on the Python language
[ Previous ] [ Next ]