使用bottle进行web开发(2):http request

我们知道,http request有多个方法,比如get,post,delete,patch,put等。对用的,bottle都定义了相应的装饰器,目前定义了五个:

get(),post(),put(),delete(),patch()

post主要用于form的submit等,这里举例如下,先定义get:

from bottle import Bottle,run,template,request

@app.get('/login')
def login():
    return '''<form action="/login" method="post">
Username: <input name="username" type="text" />
Password: <input name="password" type="password" />
<input value="Login" type="submit" />
</form>'''

这段代码运行后,会出来页面,但是提交的时候,会出错,提示没有相应的处理办法,增加对应的post处理方法:

@app.post('/login')
def do_login():
    username=request.forms.get('username')
    password = request.forms.get('password')

    if(username=='wcf'):
        return "<p>welcome wcf</p>"
    else:
        return "<p>Login failed.</p>"

后续会对request.forms做更详细的额介绍

原文地址:https://www.cnblogs.com/aomi/p/7028092.html