【问题标题】:Bottle (python): post decorator is not working瓶子(python):后装饰器不起作用
【发布时间】:2014-08-17 18:12:29
【问题描述】:

我正在关注本教程http://bottlepy.org/docs/dev/tutorial_app.html

路由效果很好,我没有尝试过路由装饰器 GET 和 POST 参数,因为我在那个教程中找到了更优雅的方式。 我使用 get 和 post 装饰器,但在 post 上出现错误 - 405,不允许使用方法。为什么?我该如何解决?

import os

# setup global and environ variables
app_root = os.path.dirname(os.path.abspath(__name__))
os.environ['FAMILY_BUDGET_ROOT'] = app_root

from bottle import route, run, redirect, request, get, post, static_file
from controller import check_login, get_page


# static section

@route('/<filename:re:.*\.css>')
def stylesheets(filename):
    return static_file(filename, root=app_root)

# dynamic section

@route('<path:path>')
def family_budget(path):
    redirect('/family_budget/login')

@get('/family_budget/login')
def login():
    username = request.get_cookie("account", secret='very_secret_key')
    if username:
        redirect('/family_budget/main_page')
    else:
        login_page = get_page('templates/login_page.html')
        return login_page

@post('/family_budget/login')
def do_login():
    username = request.forms.get('username')
    password = request.forms.get('password')
    if check_login(username, password):
        request.set_cookie("account", username, secret='very_secret_key')
        redirect('/family_budget/main_page')
    else:
        return "<p>Login failed.</p>"


run(host='0.0.0.0', port=5050)

【问题讨论】:

    标签: python bottle


    【解决方案1】:

    对 POST 路由的请求必须是 HTTP POST - 例如来自网页上的 &lt;form action="/family_budget/login" method="post"&gt;。如果你只是在浏览器中输入 URL,请求将是 HTTP GET 而不是 POST,这就是错误 405 的意思。

    【讨论】:

    猜你喜欢
    • 2016-03-14
    • 1970-01-01
    • 1970-01-01
    • 2011-01-15
    • 2016-07-30
    • 2012-07-14
    • 2017-09-28
    相关资源
    最近更新 更多