2016-11-27 80 views
0

從瓶子使用「url」功能時遇到問題。沒有這個功能,我可以提供靜態文件。 請參見下面的一些代碼:使用「url」功能的靜態文件路由名稱問題

$ cat app.py 
[...] 
@app.route('<:re:.*/><filename:path>', name='static') 
def static(filename): 
    _, ext = os.path.splitext(filename) 
    try: 
     path = _STATIC_PATH[ext] 
    except: 
     return bottle.abort(404) 

    root = os.path.join(_ROOT_PATH, 'static', path) 

    return bottle.static_file(filename, root=root) 

[...] 
@app.route('/my/file', method='GET') 
def file(): 

    return bottle.template('file', url=bottle.url) 
[...] 

$ cat file.tpl 
<script type="text/javascript" src="{{ url('static', filename='tinymce.min.js') }}" ></script> 

當運行在調試模式之前的代碼中,我得到了以下異常:

Exception: 

RouteBuildError('No route with that name.', 'static') 
Traceback: 

Traceback (most recent call last): 
    File "/usr/lib64/python2.7/site-packages/bottle.py", line 862, in _handle 
    return route.call(**args) 
    File "/usr/lib64/python2.7/site-packages/bottle.py", line 1729, in wrapper 
    rv = callback(*a, **ka) 
    File "extrapp.py", line 45, in check_session 
    return handler(**kwargs) 
    File "app.py", line 156, in news_add 
    return bottle.template('file', url=bottle.url) 
    File "/usr/lib64/python2.7/site-packages/bottle.py", line 3592, in template 
    return TEMPLATES[tplid].render(kwargs) 
    File "/usr/lib64/python2.7/site-packages/bottle.py", line 3396, in render 
    self.execute(stdout, env) 
    File "/usr/lib64/python2.7/site-packages/bottle.py", line 3383, in execute 
    eval(self.co, env) 
    File "/home/foo/dev/myapp/views/file.tpl", line 17, in <module> 
    <script type="text/javascript" src="{{ url('static', filename='tinymce.min.js') }}" ></script> 
    File "/usr/lib64/python2.7/site-packages/bottle.py", line 2689, in wrapper 
    return getattr(app(), name)(*a, **ka) 
    File "/usr/lib64/python2.7/site-packages/bottle.py", line 766, in get_url 
    location = self.router.build(routename, **kargs).lstrip('/') 
    File "/usr/lib64/python2.7/site-packages/bottle.py", line 403, in build 
    if not builder: raise RouteBuildError("No route with that name.", _name) 
RouteBuildError: ('No route with that name.', 'static') 

那麼,缺少的東西......但我定義的「靜態」名稱在路線中。你知道發生了什麼事嗎?

回答

-1

問題解決了:

  • 使用GET_URL,而不是網址
  • 使用myapp.get_url代替bottle.url
  • 刪除的 「再:* /」 靜態功能

感謝一個IRC傢伙;)