Web2py: Using images in HTML data

763 views
Skip to first unread message

Joe Magaro

unread,
Apr 16, 2015, 8:31:37 PM4/16/15
to web...@googlegroups.com
Hello,

I have an application that uses a rich textbox to insert images into a field saved in a table.

So for the image URL, I would have to put "/MySiteName/static/images/image.gif" as an example, then save to the database.

I really don't like hardcoding the site name, if I copy the project to a new one (as I often do) I need to scrub the database and change all entries to match.

Is there a way around this?

Annet

unread,
Apr 17, 2015, 3:01:17 AM4/17/15
to web...@googlegroups.com
Hi Joe,

In the book is an example of how to embed an image using helpers:


A(IMG(_src=URL('static','logo.png'), _alt="My Logo"), _href=URL('default','index'))


In one of my apps I upload images to a separate app and within that app to separate folders, to
embed these images I use:

<img src="{{=UPLOADSDOMAIN}}/nodeID{{='%s' %str(request.args(0))}}/{{=row.image}}" class="img-responsive" />

where UPLOADSDOMAIN is defined as a constant in a module and references:

'/dbmodel/static/uploads'

and nodeID{{='%s' %str(request.args(0))}} references the separate folder and
row.image contains image_name.png


Kind regards,

Annet

Joe Magaro

unread,
Apr 17, 2015, 9:14:59 AM4/17/15
to web...@googlegroups.com
Thanks Annet: I am aware of web2py URL helpers. I use them inside of my views.

However in this case, they wont work for me as the data is stored in a table and output to a web page, I require another method.

Joe Magaro

unread,
Apr 18, 2015, 9:18:16 PM4/18/15
to web...@googlegroups.com
Does anyone have a method for this?

Niphlod

unread,
Apr 18, 2015, 10:57:00 PM4/18/15
to web...@googlegroups.com
can't you just use some templating and parse the richbox text, turning /mysitename/static/image.png to /%whatever%/static/image.png ,store that on db, and upon rendering replacing %whatever% with request.application ?

Sébastien Loix

unread,
Apr 20, 2015, 6:55:19 PM4/20/15
to web...@googlegroups.com
That's what I thought too....

And if it's all coming from static, why don't you just keep in db the path from inside the static folder ( in your case "images/image.gif") and then in your view you build the path <img src="{{appname}}/'static/{{path_from_db}}" />
Reply all
Reply to author
Forward
0 new messages