【问题标题】:multiple routes and function in bottle framework瓶子框架中的多条路线和功能
【发布时间】:2017-05-08 08:18:40
【问题描述】:

我正在尝试修改已经存在的代码,只需添加表单即可添加照片。

蟒蛇:

@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>

服务器日志: 回溯(最近一次通话最后): _handle 中的文件“/home/empty/python/bottle/lib/python3.5/site-packages/bottle.py”,第 862 行 返回 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 行,在包装器中 结果 = func(*args, **kwargs)

文件“/home/empty/python/bottle/src/photo_gallery/app.py”,第 50 行,在 upload_func 中 名称,分机 = os.path.splitext(upload.filename) AttributeError:“NoneType”对象没有属性“文件名” 127.0.0.1 - - [22/Dec/2016 23:20:42] "GET /photos/add HTTP/1.1" 500 751

【问题讨论】:

    标签: python forms post bottle


    【解决方案1】:

    您已将 url 路径 /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

    还请注意,代码不应该这样写——太复杂了

    【讨论】:

      猜你喜欢
      • 2020-02-03
      • 2013-05-23
      • 1970-01-01
      • 2013-06-21
      • 2018-11-26
      • 1970-01-01
      • 2012-06-28
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多