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

生来就可爱ヽ(ⅴ<●) 提交于 2020-01-06 05:05:28

问题


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?


回答1:


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.




回答2:


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'.



来源:https://stackoverflow.com/questions/52463652/what-is-the-way-to-reference-an-image-from-within-a-bottle-template

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!