flask 中get和post用法

get和post
1、get请求:
        使用场景:如果只对服务器获取数据,并没有对服务器产生任何影响,那么这时候使用get请求
        传参:get请求传参是放在url中,并且是通过?的形式来指定key和value的
2、post请求:
        使用场景:如果要对服务器产生影响,那么使用post请求
        传参:post请求传参不是放在URL中,是通过form data 的形式发送给服务器的
get 其他年轻是通过flask.request.args来获取
post请求是通过flask.request.form来获取
post请求在模板中要注意几点:
*input 标签中,要写name来表示这个value的key,方便后台获取
*在写form表单的时候,要指定method=‘post’,并且要指定action='/login/'  

1、get实例:
模板文件index.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>首页</title>
</head>
<body>
<!--一旦点击连接就会访问视图函数search,通过视图函数来反转URL,URL的参数是q,值为hello:就会返回url:http://127.0.0.1:5000/search/?q=hello-->
    <a href="{{ url_for('search',q='hello') }}">跳转到搜索页面</a>
</body>

</html>

get_demo.py文件:

#encoding:utf-8
from flask import Flask,render_template,request

app = Flask(__name__)


@app.route('/')
def index(): #一访问127.0.0.1:5000就会返回index模板中的链接”跳转到搜索页面”
    return render_template('index.html')
@app.route('/search/')
def search():
    #arguments
    print request.args #获取所有参数
    print request.args.get('q') #或者参数为q的值
    return 'search'

@app.route('/login/',methods=['GET','POST'])
def login():
    if request.method == 'GET': #如果请求方法时GET,返回login.html模板页面
        return render_template('login.html')
    else:
        username = request.form.get('username')
        password = request.form.get('password')


if __name__ == '__main__':
    app.run()
2、post实例


login.html模板文件:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>首页</title>
</head>
<body>
    <form action="{{ url_for('login') }}" method="post">
        <table>
            <tbody>
                <tr>
                    <td>用户名:</td>
                    <td><input type="text" placeholder="请输入用户名" name="username"></td>
                </tr>
                <tr>
                    <td>密码:</td>
                    <td><input type="text" placeholder="请输入密码" name="password"></td>
                </tr>
                <tr>
                    <td></td>
                    <td><input type="submit" value="登陆"></td>
                </tr>
            </tbody>

        </table>

    </form>
</body>
</html>
get_post_demo.py文件
#encoding:utf-8
from flask import Flask,render_template,request

app = Flask(__name__)


@app.route('/')
def index(): #一访问127.0.0.1:5000就会返回index模板中的链接”跳转到搜索页面”
    return render_template('index.html')
@app.route('/search/')
def search():
    #arguments
    print request.args #获取所有参数
    print request.args.get('q') #或者参数为q的值
    return 'search'

@app.route('/login/',methods=['GET','POST'])  #指定访问页面的方法
def login():
    if request.method == 'GET': #如果请求方法时GET,返回login.html模板页面
        return render_template('login.html')
    else:
        username = request.form.get('username')
        password = request.form.get('password')
        return 'post request'

if __name__ == '__main__':
    app.run()




  • 1
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Flash是一种基于浏览器的多媒体技术,用于创建动画、交互式应用程序和其他富媒体内容。在Python,可以使用Flask框架来创建Web应用程序,包括使用Flash来显示消息。 Flask的Flash是一种用于在Web应用程序显示消息的技术。它通常用于向用户显示操作结果、错误消息或其他通知。 下面是一个使用Flask的Flash的例子: ```python from flask import Flask, flash, redirect, render_template, request, url_for app = Flask(__name__) app.secret_key = 'secret_key' @app.route('/') def index(): return render_template('index.html') @app.route('/login', methods=['GET', 'POST']) def login(): if request.method == 'POST': username = request.form['username'] password = request.form['password'] if username == 'admin' and password == 'password': flash('Login successful!') return redirect(url_for('index')) else: flash('Invalid username or password!') return render_template('login.html') if __name__ == '__main__': app.run(debug=True) ``` 在这个例子,我们使用了Flask的`flash`函数来显示消息。在登录页面,如果用户输入的用户名和密码正确,我们会使用`flash`函数显示一条“登录成功”的消息。如果用户名或密码不正确,则会显示一条“无效的用户名或密码”的消息。在`index`页面,我们可以使用`get_flashed_messages`函数来获取已经显示的消息。 ```html {% for message in get_flashed_messages() %} <div class="alert alert-info"> {{ message }} </div> {% endfor %} ``` 这个例子只是Flash在Python的一个简单应用。在实际应用,Flash可以用于更多的场景,比如显示表单验证错误、成功提交表单等等。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值