简体   繁体   English

如何在URL的Python瓶中加载静态文件

[英]How to load static files in python bottle in URL's

I'm working on a python app in Python Bottle. 我正在使用Python Bottle中的python应用程序。 The app works fine if I'm on 1 lvl deep URLs like /dashboard or /rules or /page. 如果我使用像/ dashboard或/ rules或/ page这样的1个lvl深度URL,该应用程序可以正常工作。 However, if I go deeper like /dashboard/overview or /rules/ruleone or /page/test the CSS, JS, fonts and images will fail. 但是,如果我更深入地像/ dashboard / overview或/ rules / ruleone或/ page / test那样,CSS,JS,字体和图像将会失败。 :( :(

The HTML source code still poinsts to /assets/ but if I'm on an URL like /rules/ruleone, the right path should be something like ../assets or ./assets right? HTML源代码仍然指向/ assets /,但是如果我使用的是/ rules / ruleone之类的URL,则正确的路径应该是../assets或./assets之类的东西吗? The path /assets/ only works on the first level but not on deeper lvls, in other words: bottle doesnt adapt the static file path to the current directory. path / assets /仅适用于第一级,但不适用于更深层的lvls,换句话说:bottle不会将静态文件路径调整到当前目录。 How do I fix this? 我该如何解决?

I'm stuck on this problem for days now, I realy hope someone can help me. 我已经在这个问题上待了好几天了,我真的希望有人能帮助我。 :( :(

My code (simplified): 我的代码(简化):

#!/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()

So my app runs in daemon mode. 所以我的应用程序以守护进程模式运行。 The structure is: 结构是:

lib LIB

  • bottle.py bottle.py
  • bottledaemon.py bottledaemon.py

assets 资产

  • css CSS
  • js JS
  • ... ...

view 视图

  • secure_page.tpl secure_page.tpl
  • footer.tpl footer.tpl
  • header.tpl header.tpl
  • ... ...

server.py server.py

I hope someone can help me out on this, thanks in advance guys, I love u! 我希望有人可以帮我解决这个问题,在此先感谢大家,我爱你! <3 <3

Alright I found the solution for my problem. 好吧,我找到了解决问题的方法。 Bottle offers an URL tag to dynamicly build URLs. 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')

This is how I load my CSS/JS/images 这就是我加载CSS / JS / images的方式

<link href="{{ url('assets', filepath='css/style.css') }}" rel="stylesheet" type="text/css"/>

Dynamic menu URL's (in the navigation for example) is done this way: 动态菜单URL(例如在导航中)以这种方式完成:

{{ url('/dashboard') }}

I hope this info will help someone who is strugling with the same problem as I was. 我希望此信息对与我一样遇到同样问题的人有所帮助。

Tested on v0.12 and v0.13dev 在v0.12和v0.13dev上测试过

I do use: 我用的是:

@route('/css/<filename>')
def stylesheets(filename):
return static_file(filename, root='./static/css/')

and in template use: 并在模板中使用:

<link href="/css/style.css" rel="stylesheet">

can be replicated for all assets items (img, css, and js) 可以复制所有资产项目(img,css和js)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM