简体   繁体   中英

Bottle (python): post decorator is not working

I was following this tutorial 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. I uses get and post decorators, but on post I had an error - 405, method is not allowed. 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. If you just enter the URL in browser, the request will be HTTP GET instead of POST, which is what is error 405 saying.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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