What is the way to reference an image from within a bottle template?

61 views
Skip to first unread message

nikos.at....@gmail.com

unread,
Sep 23, 2018, 8:00:12 AM9/23/18
to bottlepy

When within a bottle template file, what is the way to reference a static file? for example this one?

<body background='/static/img/digital.gif'>

Why the above relative file fails to load the image? How can i reference it correctly? If i try it as:

<body background="{{ static_file( 'digital.gif', root='./static/img' }}">

The image also fail to render.

Why Bottle fail to render the image even if no 'static_file' function is used?

Marcel Hellkamp

unread,
Sep 23, 2018, 8:19:07 AM9/23/18
to bott...@googlegroups.com
Am 23.09.2018 um 09:05 schrieb nikos.at....@gmail.com:

When within a bottle template file, what is the way to reference a static file? for example this one?

<body background='/static/img/digital.gif'>

Why the above relative file fails to load the image?


Th above example instructs the browser to load the image from http://whatever.your-domain.is/static/img/digital.gif. Make sure the image is reachable under that URL. You need something like this on server site:

@route('/static/<filename:path>')
def server_static(filename):
    return static_file(filename, root='/path/to/your/static/files')

By the way, the '<body background=...>' is deprecated and not supported in HTML5. If your template generates a HTML5 file, the browser might just ignore unsupported tags or attributes it.


How can i reference it correctly? If i try it as:

<body background="{{ static_file( 'digital.gif', root='./static/img' }}">

The image also fail to render.


How is that supposed to work?

nikos.at....@gmail.com

unread,
Sep 26, 2018, 12:17:38 PM9/26/18
to bottlepy
The image is not reachbale iam afraid i do NOT understand why.

DocumentRoot is '/home/nikos/public_html' and the images folder is under '/home/nikos/public_html/static/img'  but when i request:

Sorry, the requested URL 'http://superhost.gr/static/img/digital.gif' caused an error:

Not found: '/static/img/digital.gif'

Why would that happen?

Νικόλαος Κούρας

unread,
Sep 26, 2018, 12:17:39 PM9/26/18
to bott...@googlegroups.com
The images is not reachable under that url.

my DocumentRoot is '/home/nikos/public_html'
Why can't even i see the image as http://superhost.gr/static/img/digital.gif

The images are in '/home/nikos/public_html/static/img/' folder

--
--
You are member of the "bottlepy" group at google groups.
See http://groups.google.de/group/bottlepy for mailing list options.
See http://bottlepy.org/ for news and documentation.

---
You received this message because you are subscribed to the Google Groups "bottlepy" group.
To unsubscribe from this group and stop receiving emails from it, send an email to bottlepy+u...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Marcel Hellkamp

unread,
Sep 26, 2018, 12:37:15 PM9/26/18
to bott...@googlegroups.com

On 09/23/2018 02:47 PM, nikos.at....@gmail.com wrote:
> The image is not reachbale iam afraid i do NOT understand why.
>
> DocumentRoot is '/home/nikos/public_html' and the images folder is
> under '/home/nikos/public_html/static/img'  but when i request:

Did you add a route for static files, as I told you in my first reply?
Bottle does not magically serve files from disk.
You need to explicitly add a route to serve static files, as documented
here: http://bottlepy.org/docs/dev/tutorial.html#routing-static-files

@route('/static/<filename:path>')
def server_static(filename):
    return static_file(filename, root='/home/nikos/public_html/static/')
Reply all
Reply to author
Forward
0 new messages