简体   繁体   English

瓶框架中的多种途径和功能

[英]multiple routes and function in bottle framework

I'm trying to modify already existing code, simply add form to add photos. 我正在尝试修改已经存在的代码,只需添加表单即可添加照片。

python: 蟒蛇:

@route('/photos/add')
@jinja_view('add.html')
@post('/photos/add')
def upload_func():
    upload = request.files.get('pic')
    name, ext = os.path.splitext(upload.filename)
    if ext not in ('.png', '.jpg', '.jpeg'):
        return "ext is not allowed"
    save_path = "/src/photo_gallery/photos"
    upload.save(save_path)
    return "photo is saved"

HTML: HTML:

<form action="/photos/add" method="post">
    <div align="center">
        <label>Picture</label>
        <input type="file" name="pic" required>
    </div>
    <div>
        <label>Info</label>
        <input type="text" name="text">
    </div>
    <div>
        <input type="submit" value="add">
    </div>
</form>

server log: Traceback (most recent call last): File "/home/empty/python/bottle/lib/python3.5/site-packages/bottle.py", line 862, in _handle return route.call(**args) 服务器日志:追溯(最近一次调用):文件“ /home/empty/python/bottle/lib/python3.5/site-packages/bottle.py”,行862,在_handle返回route.call(** args )

File "/home/empty/python/bottle/lib/python3.5/site-packages/bottle.py", line 1740, in wrapper rv = callback(*a, **ka) 在包装器rv = callback(* a,** ka)中的文件“ /home/empty/python/bottle/lib/python3.5/site-packages/bottle.py”,行1740

File "/home/empty/python/bottle/lib/python3.5/site-packages/bottle.py", line 3635, in wrapper result = func(*args, **kwargs) 文件“ /home/empty/python/bottle/lib/python3.5/site-packages/bottle.py”,第3635行,包装结果= func(* args,** kwargs)

File "/home/empty/python/bottle/src/photo_gallery/app.py", line 50, in upload_func name, ext = os.path.splitext(upload.filename) AttributeError: 'NoneType' object has no attribute 'filename' 127.0.0.1 - - [22/Dec/2016 23:20:42] "GET /photos/add HTTP/1.1" 500 751 文件“ /home/empty/python/bottle/src/photo_gallery/app.py”,第50行,在upload_func名称中,ext = os.path.splitext(upload.filename)AttributeError:“ NoneType”对象没有属性“ filename” '127.0.0.1--[2016年12月22日23:20:42]“ GET / photos / add HTTP / 1.1” 500751

You have linked the url path /photos/add to the callback function upload_func . 您已将url路径/photos/add到回调函数upload_func It looks like you want to support two request types (GET and POST), then function decorators should look like this: 看起来您想支持两种请求类型(GET和POST),然后函数装饰器应如下所示:

@route('/photos/add', method=['GET', 'POST'])
@jinja_view('add.html')
def upload_func():
    # ...

Take a look at: 看一眼:

https://bottlepy.org/docs/dev/tutorial.html#request-routing https://bottlepy.org/docs/dev/api.html#bottle.Bottle.route https://bottlepy.org/docs/dev/tutorial.html#request-routing https://bottlepy.org/docs/dev/api.html#bottle.Bottle.route

Please also note that the code should not be written like this - too complex 另请注意,代码不应这样写-太复杂了

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

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