New to Web2py, so my question might not be too clear. I'm trying to make a shipment tracking page, and I have a simple database with a tracking number and a shipper ID. Following the examples, my application can display and add new records to the shipment db. Now I want to add links to the displayed records that will take you to the carrier's tracking page for that tracking number. The tracking page URL will be created from a default string for each shipper to which the tracking number will be appended. How can I make the results into links to the appropriate tracking page?
3 Answers
If you are displaying the record via SQLFORM, Crud, SQLTABLE, or SQLFORM.grid, you could set the "represent" attribute of the tracking number field to display a link:
db.define_table('shipper',
Field('name'),
Field('url'),
format='%(name)s')
db.define_table('shipment',
Field('tracking_number', represent=lambda value, row: A(value,
_href=row.shipper.url + value)),
Field('shipper', db.shipper))
This assumes you are storing the shipper URLs in a separate table.

- 25,466
- 3
- 28
- 57
-
Getting this error when I try to add a shipment: AttributeError: 'NoneType' object has no attribute 'url' – user1011625 May 01 '12 at 14:44
-
Thanks for the suggestion! I was able to tweak it and make it work with some help from the folks at the [Web2py user group](https://groups.google.com/forum/?fromgroups#!topic/web2py/Z1Zng7WmECE). Check the answer below for the final version. – user1011625 May 01 '12 at 19:31
I changed it to this, and it almost works:
db.define_table('carrier',
Field('name',),
Field('url',),
format='%(name)s')
db.define_table('shipment',
Field('shipment_id', represent=lambda value,row: \
A(value, _href=(row.carrier.url + value, ))),
Field('carrier', db.carrier))
shipment_id is a link, but it points to 'http://127.0.0.1:8000/tracker/default/www.bing.com/search?q=trumpet', where 'www.bing.com/search?q=' and 'trumpet' are the carrier.url and shipment.shipment_id values respectively (just for testing). How can I leave off 'http://127.0.0.1:8000/tracker/default/'?

- 121
- 2
- 10
Ok, ended up with:
db.define_table('carrier',
Field('name',),
Field('url',),
format='%(name)s')
db.define_table('shipment',
Field('shipment_id', represent=lambda value,row: \
A(value, _href=row.carrier.url + value)),
Field('carrier', db.carrier))
There were two problems. First was the syntax of the href. The version in the answer I added above didn't give an error, but caused the link address problem. Second, the URLs were being entered without the "http://". This caused the same link address issue.

- 121
- 2
- 10