user3351605
user3351605

Reputation: 1291

Dynamic Routing With Bottle.py

I'm very new to the Bottle framework and am having a hard time understanding what I am doing wrong when trying to serve static files using dynamic routes.

The following works just fine for me when I use exact values:

@route('/files/somefile.txt')
def serve_somefile():
   return static_file('somefile.txt', root = '/directory/to/files')

However, I am trying to create a dynamic route to serve any file in the /files directory based on the documentation.

This does not work for me:

@route('/files/<filename>')
def serve_somefile(filename):
   return static_file(filename, root= '/directory/to/files')

I get a 404 response from the server, despite it receiving an identical GET request compared to the above example.

Can anyone point out what I'm doing wrong here?

Upvotes: 3

Views: 3010

Answers (2)

ron rothman
ron rothman

Reputation: 18128

Nothing in your code looks wrong to me. (And I agree with @Ashalynd that you should be using :path here.)

In fact, I tried running your code, and both cases work.

Perhaps you're using an old version of Bottle? I'm on 0.12.7.

--

Here's my complete example, in case it helps:

import bottle
from bottle import route, static_file

@route('/files/<filename>')
def serve_somefile(filename):
    return static_file(filename, root= '/Users/ron/Documents/so/25043651')

bottle.run(host='0.0.0.0', port=8080)

Upvotes: 0

Ashalynd
Ashalynd

Reputation: 12563

Did you try specifying the parameter as path (like in their example):

@route('/files/<filename:path>')
def serve_somefile(filename):
    return static_file(filename, root= '/directory/to/files')

Upvotes: 2

Related Questions