1

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?

2 Answers2

2

You should add a route to your application that will return static files when they are requested. Then use the paths in html as normal paths.

So add this:

@app.route('/static/<filepath:path>')
def server_static(filepath):
    return static_file(filepath, root='/path/to/static')

then accessing: http://localhost:8080/static/file.txt will return '/path/to/static/file.txt'.

Sub-folders of '/path/to/static' will also be accessible.

Eduard
  • 897
  • 5
  • 11
  • What do you mean by return static files? I just want images to be displayed in the browser. Do i need a function for that? I'am using Apache production server not the embedded development server and i cant even http://superhost.gr/static/img/digital.gif for example. The path is correct i just do NOT understand why the file is NOT being displayed in the browser. If the url cannot be views in the browser HOW Bottle will be bale to display it? Please tell em what you want me to try! – Νικόλαος Βέργος Sep 23 '18 at 18:26
  • ok, then you don't need this, but you have to configure apache to serve static files separately from webapp? something like done here: http://webpy.org/cookbook/staticfiles . "404 Not Found" that you have at superhost.gr/static/img/digital.gif is coming from bottle. and is correct, as it has no route defined. But if you configure apache correctly (so that it knows that '/static/' subfolder is served from filesystem) then bottle will not get the request at all. – Eduard Sep 23 '18 at 18:36
  • So i have to `Alias /static /home/nikos/public_html/static' within my httpd-vhosts.conf ? Could you please elaborate for me what is causing the error here? Why Bottle want to take over on serving static files and doesn't let Apache do it? When i was using Flask i wasn;t in need for such separation? Why Bottle want to handle static files and treat them as routes? – Νικόλαος Βέργος Sep 23 '18 at 18:45
  • This is not a problem with Bottle. You can even try to remove it, or whatever. If you configure apache properly requests to static file should go directly through apache and filesystem file should be returned. Apache configuration should tell apache to only ask Bottle for request processing when something other than static file is requested. See similar apache setup question here https://stackoverflow.com/questions/24739925/serving-static-files-through-apache. – Eduard Sep 23 '18 at 18:50
  • Yes it works now that explicitly told Apache HOW to handle 'static' folder, thank you for that! It's just when i was using Flask i didn't had to make that distinction. Apache was serving them there. – Νικόλαος Βέργος Sep 23 '18 at 19:01
  • Could you please tell me HOW to enable error output to the browser instead of logginf via ssh all the time and 'tail -f error_log' In dev instance we just did 'debug =True', now in the production server? – Νικόλαος Βέργος Sep 23 '18 at 19:13
  • If this helped you, please mark answer as accepted (feel free to post you final config too), and open another question if needed. thanks. – Eduard Sep 23 '18 at 19:14
  • I want to post my config Edward. but when i answer in comments i do not know how to leave an empty line between lines. `
    ' wont do. How do i i insert an empty line between comments so my post would not seen as a long string ?
    – Νικόλαος Βέργος Sep 23 '18 at 19:31
  • You can edit your question or my answer, or add own- answer – Eduard Sep 23 '18 at 19:44
0

i'm trying to figure out how to enable error output to the browser instead of logginf via ssh all the time and 'tail -f error_log' ?

In dev instance we just did debug =True, now in the production server? Here is my config:

DocumentRoot /home/nikos/public_html

<Directory /home/nikos/public_html>
    Require all granted
</Directory>


Alias /static /home/nikos/public_html/static

<Directory /home/nikos/public_html/static>
    Options +Indexes
</Directory>


WSGIPassAuthorization On

WSGIDaemonProcess clientele user=nikos group=nikos home=/home/nikos/public_html
WSGIScriptAlias /clientele /home/nikos/public_html/clientele.py process-group=clientele application-group=%{GLOBAL}

WSGIDaemonProcess downloads user=nikos group=nikos home=/home/nikos/public_html
WSGIScriptAlias /downloads /home/nikos/public_html/downloads.py process-group=downloads application-group=%{GLOBAL}

WSGIDaemonProcess www user=nikos group=nikos home=/home/nikos/public_html
WSGIScriptAliasMatch ^/(?!phpmyadmin) /home/nikos/public_html/www.py process-group=www application-group=%{GLOBAL}

Also i would like to know how how to grab the authuser value entered in authentication prompt so to have it stored when entered. request.auth.user' won't return it norrequest.auth.username'.