2016-11-27 77 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家伙;)