flask(web服务器)—Python(四)
小知识
pip list
flask 端口5000
Flask的路由和反向路由(推荐chrome的postman插件)
from flask import Flask,request,url_for
app = Flask(__name__)
@app.route('/')
def hello_world():
return 'Hello World!'
@app.route('/user', methods=['POST']) # 这个和浏览器都默认是get方法,用postman插件,选post方法
def hello_user():
return 'hello user'
@app.route('/users/<id>') # 访问格式 http://127.0.0.1:5000/users/2
def user_id(id):
return 'hello user:'+id
@app.route('/query_user') # 访问格式 http://127.0.0.1:5000/query_user?id=2
def query_user1():
id = request.args.get('id')
return 'query user:'+id
@app.route('/query_url') # 反向路由
def query_url():
return 'query url:'+url_for('query_user1')
if __name__ == '__main__':
app.run()
Flask的模板
from flask import Flask, render_template
from models import User
app = Flask(__name__)
@app.route('/')
def hello_world():
content = "Hello world"
return render_template("index.html",content=content)
# <!DOCTYPE html>
# <html>
# <head lang="en">
# <meta charset="UTF-8">
# <title></title>
# </head>
# <body>
# <h1>{{ content }}</h1>
# </body>
# </html>
@app.route('/user')
def user_index():
user = User(1, 'jikexueyuan')
return render_template("user_index.html", user=user)
# <!DOCTYPE html>
# <html>
# <head lang="en">
# <meta charset="UTF-8">
# <title></title>
# </head>
# <body>
# <h1>hello {{ user.user_name }}</h1>
# </body>
# </html>
@app.route('/query_user/<user_id>') #条件语句
def query_user(user_id):
user = None
if int(user_id) == 1:
user = User(1, 'jikexueyuan')
return render_template("user_id.html",user=user)
# <!DOCTYPE html>
# <html>
# <head lang="en">
# <meta charset="UTF-8">
# <title></title>
# </head>
# <body>
# {% if user %}
# hello {{ user.user_name }}
# {% else %}
# no this user
# {% endif %}
# </body>
# </html>
@app.route('/users') #循环
def user_list():
users = []
for i in range(1,11):
user = User(i, 'jikexueyuan'+str(i))
users.append(user)
return render_template("user_list.html",users=users)
# <!DOCTYPE html>
# <html>
# <head lang="en">
# <meta charset="UTF-8">
# <title></title>
# </head>
# <body>
# {% for user in users %}
# {{ user.user_id }}--{{ user.user_name }}<br>
# {% endfor %}
# </body>
# </html>
@app.route('/one') #继承
def one_base():
return render_template("one_base.html")
@app.route('/two')
def two_base():
return render_template("two_base.html")
# <!DOCTYPE html>
# <html>
# <head lang="en">
# <meta charset="UTF-8">
# <title></title>
# </head>
# <body>
# <div>
# <h1>Header 极客学院</h1>
# </div>
# {% block content %}
# {% endblock %}
# <div>
# <h1>Footer jikexueyuan</h1>
# </div>
# </body>
# </html>
# one_base.html如下
# {% extends "base.html" %}
# {% block content %}
# <h2>这是第一页</h2>
# {% endblock %}
# two_base.html如下
# {% extends "base.html" %}
# {% block content %}
# <h2>这是第二页</h2>
# {% endblock %}
# if __name__ == '__main__':
# app.run()
Flask的消息提示与异常处理(代码有bug)
from flask import Flask, flash, render_template, request,abort
app = Flask(__name__)
app.secret_key = '123'
@app.route('/')
def hello_world():
flash("hello jikexueyuan")
return render_template("index.html")
@app.route('/login', methods=['POST'])
def login():
form = request.form
username = form.get('username')
password = form.get('password')
if not username:
flash("please input username")
return render_template("index.html")
if not password:
flash("please input password")
return render_template("index.html")
if username == 'jikexueyuan' and password == '123456':
flash("login success")
return render_template("index.html")
else:
flash("username or password is wrong")
return render_template("index.html")
@app.errorhandler(404)
def not_found(e):
return render_template("404.html")
@app.route('/users/<user_id>')
def users(user_id):
if int(user_id) == 1:
return render_template("user.html")
else:
abort(404)
if __name__ == '__main__':
app.run()
index.html如下:
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title></title>
</head>
<body>
<h1>Hello Login</h1>
<form action="/login" method="post">
<input type="text" name="username">
<input type="password" name="password">
<input type="submit" value="Submit">
</form>
<h2>{{ get_flashed_messages()[0] }}</h2>
</body>
</html>
404.html如下:
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title></title>
</head>
<body>
<h1>你要找的页面去火星了</h1>
<h2>抱歉!该页面不存在</h2>
</body>
</html>