瓶子(python):post decorator不工作

2024-06-07 04:28:15 发布

您现在位置:Python中文网/ 问答频道 /正文

我是按照这个教程http://bottlepy.org/docs/dev/tutorial_app.html

Route工作得很好,我还没有尝试过Route decorator GET和POST参数,因为我在那个教程中找到了更优雅的方法。 我使用get和post修饰符,但在post上出现错误-405,method is not allowed。为什么?我该怎么修?在

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)

Tags: pathappgetsecretosrequestdefpage
1条回答
网友
1楼 · 发布于 2024-06-07 04:28:15

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

相关问题 更多 >