标签:port and 目录 请求 用户登录 bubuko view http 查看
- from flask import Flask
- 从Flask中查看源码,会跳转到app.py,app.py里有class Flask,class Flask里面有__init__
- 从源码可见,默认模板路径是当前路径的templates目录
- from flask import render_template
- 在@app.route(‘path‘)装饰的view function中 return render_template(‘xxx.html‘)
- 请求都存进request对象中。 from flask import request
- from flask import redidect为重定向,例如登录成功重定向到另一个页面
- 在render_template第二个参数中写要传入模板的对象,然后在模板的{{xxx}}就是替换参数的位置
def __init__(
self,
import_name,
static_url_path=None,
static_folder=‘static‘,
static_host=None,
host_matching=False,
subdomain_matching=False,
template_folder=‘templates‘,
instance_path=None,
instance_relative_config=False,
root_path=None
):
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Login</title>
</head>
<body>
<h1>用户登录</h1>
<form method="post">
<input type="text" name="user">
<input type="password" name="password">
<input type="submit" value="login">
{{error}}
</form>
</body>
</html>
from flask import Flask, render_template, request, redirect
app = Flask(__name__)
@app.route(‘/login‘, methods=[‘GET‘, ‘POST‘])
def login():
if request.method == ‘GET‘:
return render_template(‘login.html‘)
else:
# request.query_string
user = request.form.get(‘user‘)
password = request.form.get(‘password‘)
if user == ‘allin‘ and password == ‘123456‘:
return redirect(‘https://www.baidu.com‘)
return render_template(‘login.html‘, error=‘user or password is wrong‘)
标签:port and 目录 请求 用户登录 bubuko view http 查看
原文地址:https://www.cnblogs.com/allen2333/p/9008316.html