网站开发之get请求和post请求
get请求:从服务器获取服务器的资源。只想获取服务器的数据,并未对服务器产生任何影响,就是用get请求。
get 请求传参是放在url之中,是通过?的形式来指定key和value
post请求:向服务器提交数据
post请求得传参不是放在url之中的,而是放在form data之中得
flask中get请求和post请求
<a href="{{ url_for('search',q=12) }}">跳转到搜索页面</a>
直接在视图函数中加入上面的代码,这样生成得url就附带了get函数得请求参数。
在后台查看get函数代码如下:
@app.route('/search/')
def search():
s=request.args.get("q")
print("get请求得参数是",s)
return render_template('search.html')
在视图函数中制作一张表单,使用post方法提交:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>login</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>

21.png
在后台查看提交得数据代码如xia:
@app.route('/login/',methods=['GET','POST'])
def login():
if request.method=='GET':
return render_template('login.html')
else:
username=request.form.get('username')
password=request.form.get('password')
return "用户名是:%s密码是:%s"%(username,password)
有了以上简单得操作,再将其和数据库连接起来基本实现了一些前台和后台得功能了。