我正在尝试修改现有代码,只需添加表单即可添加照片。
蟒:
@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:
<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>
服务器日志 Traceback(最近一次调用最后一次): 文件“/home/empty/python/bottle/lib/python3.5/site-packages/bottle.py”,第862行,在_handle return route.call(** args)
文件“/home/empty/python/bottle/lib/python3.5/site-packages/bottle.py”,第1740行,在包装器中 rv =回调(* a,** ka)
文件“/home/empty/python/bottle/lib/python3.5/site-packages/bottle.py”,第3635行,在包装器中 result = func(* args,** kwargs)
文件“/home/empty/python/bottle/src/photo_gallery/app.py”,第50行,在upload_func中 name,ext = os.path.splitext(upload.filename) AttributeError:'NoneType'对象没有属性'filename' 127.0.0.1 - - [22 / Dec / 2016 23:20:42]“GET / photos / add HTTP / 1.1”500 751
答案 0 :(得分:0)
您已将网址路径/photos/add
与回调函数upload_func
相关联。看起来你想要支持两种请求类型(GET和POST),然后函数装饰器应该是这样的:
@route('/photos/add', method=['GET', 'POST'])
@jinja_view('add.html')
def upload_func():
# ...
看看:
https://bottlepy.org/docs/dev/tutorial.html#request-routing https://bottlepy.org/docs/dev/api.html#bottle.Bottle.route
请注意,代码不应该这样写 - 太复杂了