简体   繁体   English

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

[英]Bottle (python): post decorator is not working

I was following this tutorial http://bottlepy.org/docs/dev/tutorial_app.html 我正在遵循本教程http://bottlepy.org/docs/dev/tutorial_app.html

Route works well, I haven't tried in route decorator GET and POST parameters, because I found more elegant way in that tutorial. Route运行良好,我没有尝试使用路由装饰器的GET和POST参数,因为我在该教程中找到了更优雅的方法。 I uses get and post decorators, but on post I had an error - 405, method is not allowed. 我使用get和post装饰器,但是在发布时出现错误-405,不允许使用方法。 Why? 为什么? How should I fix it? 我应该如何解决?

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)

The request to POST route must be HTTP POST - for example from a <form action="/family_budget/login" method="post"> on a web page. POST路由请求必须是HTTP <form action="/family_budget/login" method="post">例如,来自网页上的<form action="/family_budget/login" method="post"> If you just enter the URL in browser, the request will be HTTP GET instead of POST, which is what is error 405 saying. 如果仅在浏览器中输入URL,则请求将是HTTP GET而不是POST,这就是错误405所说的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM