瓶子(python):后装饰器不起作用

时间:2014-08-17 18:12:30

标签: python bottle

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

路由运行良好,我没有尝试路由装饰器GET和POST参数,因为我在该教程中找到了更优雅的方式。 我使用get和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)

1 个答案:

答案 0 :(得分:0)

POST路由的请求必须是HTTP POST - 例如来自网页上的<form action="/family_budget/login" method="post">。如果您只是在浏览器中输入URL,请求将是HTTP GET而不是POST,这就是错误405所说的。