使用" url"使用静态文件路由名称问题函数

时间:2016-11-27 16:33:41

标签: python bottle

使用" 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')

嗯,有些东西不见了......但我定义了静态&#39;路线中的名字。你知道发生了什么吗?

1 个答案:

答案 0 :(得分:-1)

问题解决了:

  • 使用get_url而不是url
  • 使用myapp.get_url代替bottle.url
  • 删除“re:*。/”以获取静态功能

感谢IRC的一员;)