Bottle(Python):post装饰器无法工作
我在看这个教程 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)
1 个回答
0
要发送一个POST请求,必须使用HTTP POST方法。比如在网页上有一个表单,像这样:<form action="/family_budget/login" method="post">
。如果你只是直接在浏览器里输入网址,那发送的请求就会变成HTTP GET,而不是POST,这就是错误405所指的意思。