[英]How to load static files in python bottle in URL's
我正在使用Python Bottle中的python應用程序。 如果我使用像/ dashboard或/ rules或/ page這樣的1個lvl深度URL,該應用程序可以正常工作。 但是,如果我更深入地像/ dashboard / overview或/ rules / ruleone或/ page / test那樣,CSS,JS,字體和圖像將會失敗。 :(
HTML源代碼仍然指向/ assets /,但是如果我使用的是/ rules / ruleone之類的URL,則正確的路徑應該是../assets或./assets之類的東西嗎? path / assets /僅適用於第一級,但不適用於更深層的lvls,換句話說:bottle不會將靜態文件路徑調整到當前目錄。 我該如何解決?
我已經在這個問題上待了好幾天了,我真的希望有人能幫助我。 :(
我的代碼(簡化):
#!/usr/bin/env python
import lib.bottle as bottle
from lib.bottle import route, template, debug, static_file, TEMPLATE_PATH, error, auth_basic, get, post, request, response, run, view, redirect, SimpleTemplate, HTTPError, abort
import os, sys, re
@route('/dashboard')
@view('secure_page')
def show__page_dashboard():
return dict(page = 'Dashboard')
@route('/rules/<rule>')
@view('secure_page')
def show_page_rules_more(rule):
return dict(page = rule)
@route('/assets/<filepath:path>')
def server_static(filepath):
return static_file(filepath, root='/var/myapp/assets')
TEMPLATE_PATH.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), "view")))
bottle.debug(True)
from lib.bottledaemon import daemon_run
if __name__ == "__main__":
daemon_run()
所以我的應用程序以守護進程模式運行。 結構是:
LIB
資產
視圖
server.py
我希望有人可以幫我解決這個問題,在此先感謝大家,我愛你! <3
好吧,我找到了解決問題的方法。 Bottle提供了一個URL標記來動態構建URL。
from bottle import url
@route('/dashboard')
@view('secure_page')
def show__page_dashboard():
return dict(page='Dashboard', url=url)
@route('/assets/<filepath:path>', name='assets')
def server_static(filepath):
return static_file(filepath, root='/var/myapp/assets')
這就是我加載CSS / JS / images的方式
<link href="{{ url('assets', filepath='css/style.css') }}" rel="stylesheet" type="text/css"/>
動態菜單URL(例如在導航中)以這種方式完成:
{{ url('/dashboard') }}
我希望此信息對與我一樣遇到同樣問題的人有所幫助。
在v0.12和v0.13dev上測試過
我用的是:
@route('/css/<filename>')
def stylesheets(filename):
return static_file(filename, root='./static/css/')
並在模板中使用:
<link href="/css/style.css" rel="stylesheet">
可以復制所有資產項目(img,css和js)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.