Flask学习 黑马程序员-6节课入门Flask框架web开发视频(中途撤退,寻找py3教程)
文章目錄
- postman工具
- get和post
- 如何給路由傳參 解析
- @app.route('')這個(gè)叫視圖函數(shù)
- Jinja2模板引擎
- 動(dòng)態(tài)傳參
- 注釋、變量代碼塊以及控制代碼塊的使用
- 注釋:ctrl+/
- 變量代碼塊使用
- 控制代碼塊的使用 if for等(for循環(huán))
- 過濾器的使用(比如小寫轉(zhuǎn)大寫,獲取第幾個(gè)字符之類的)
- web表單及驗(yàn)證
- flash消息閃現(xiàn),給模板傳遞消息,消息加密(做混淆用)
- 表單樣式的顯示(使用flask_wtf模板創(chuàng)建表單)
- 模板表單的驗(yàn)證 csrf_token
- Flask中使用SQL數(shù)據(jù)庫 SQLAlchemy(flask_sqlalchemy)(如果是使用mysql數(shù)據(jù)庫,安裝flask_mysqldb)
- mysql數(shù)據(jù)模型的定義 常用SQLAlchemy字段類型/列選項(xiàng)/關(guān)系選項(xiàng)
- 數(shù)據(jù)庫基本操作,增刪改 db.session
Python-flask視頻網(wǎng)站搭建
用flask-做一個(gè)資源網(wǎng)站(part1)-項(xiàng)目
黑馬程序員-6節(jié)課入門Flask框架web開發(fā)視頻
查看虛擬環(huán)境
postman工具
https://www.postman.com/
get和post
如何給路由傳參 解析
@app.route(’’)這個(gè)叫視圖函數(shù)
Jinja2模板引擎
動(dòng)態(tài)傳參
flask_template_demo.py
# -*- coding: utf-8 -*- """ @File : flask_template_demo.py @Time : 2021/1/26 16:41 @Author : Dontla @Email : sxana@qq.com @Software: PyCharm """ from flask import Flask, render_templateapp = Flask(__name__)@app.route('/') def hello_world():url_str = 'www.baidu.com'return render_template('index.html', url_str=url_str)if __name__ == '__main__':app.run()index.html
<!DOCTYPE html> <html lang="en"> <head><meta charset="UTF-8"><title>FLASK TEST</title> </head> <body>這是模板<br>這是首頁<br>{{url_str}} <br> </body> </html>注釋、變量代碼塊以及控制代碼塊的使用
注釋:ctrl+/
<!---->變量代碼塊使用
# -*- coding: utf-8 -*- """ @File : flask_template_demo.py @Time : 2021/1/26 16:41 @Author : Dontla @Email : sxana@qq.com @Software: PyCharm """ from flask import Flask, render_templateapp = Flask(__name__)@app.route('/') def hello_world():url_str = 'www.baidu.com'my_list = [1, 2, 3, 4, 5]my_dict = {'name': '黑馬', 'url': 'www.tencent.com'}return render_template('index.html', url_str=url_str, my_list=my_list, my_dict=my_dict)if __name__ == '__main__':app.run() <!DOCTYPE html> <html lang="en"> <head><meta charset="UTF-8"><title>FLASK TEST</title> </head> <body> 這是模板<br> 這是首頁<br> {{url_str}} <br> {{my_list}} <br> {{my_list.2}} <br> {{my_list[2]}} <br> {{my_dict}} <br> {{my_dict.name}} <br> {{my_dict['name']}} <br> </body> </html>控制代碼塊的使用 if for等(for循環(huán))
<!DOCTYPE html> <html lang="en"> <head><meta charset="UTF-8"><title>FLASK TEST</title> </head> <body> 這是模板<br> 這是首頁<br> {{url_str}} <br> {{my_list}} <br> {{my_list.2}} <br> {{my_list[2]}} <br> {{my_dict}} <br> {{my_dict.name}} <br> {{my_dict['name']}} <br><hr>{% for i in my_list %}{% if i >=3 %}{{i}} <br>{% endif %} {% endfor %} </body> </html> # -*- coding: utf-8 -*- """ @File : flask_template_demo.py @Time : 2021/1/26 16:41 @Author : Dontla @Email : sxana@qq.com @Software: PyCharm """ from flask import Flask, render_templateapp = Flask(__name__)@app.route('/') def hello_world():url_str = 'www.baidu.com'my_list = [1, 2, 3, 4, 5]my_dict = {'name': '黑馬', 'url': 'www.tencent.com'}return render_template('index.html', url_str=url_str, my_list=my_list, my_dict=my_dict)if __name__ == '__main__':app.run()過濾器的使用(比如小寫轉(zhuǎn)大寫,獲取第幾個(gè)字符之類的)
web表單及驗(yàn)證
# -*- coding: utf-8 -*- """ @File : flask_WTF.py @Time : 2021/1/27 14:10 @Author : Dontla @Email : sxana@qq.com @Software: PyCharm """ from flask import Flask, render_template, requestapp = Flask(__name__)@app.route('/', methods=["GET", "POST"]) def index():if request.method == 'POST':username = request.form.get('username')password = request.form.get('password')password2 = request.form.get('password2')if not all([username, password, password2]):return '參數(shù)不完整'else:return 'success'return render_template('index_WTF.html')if __name__ == '__main__':app.run(debug=True) <!DOCTYPE html> <html lang="en"> <head><meta charset="UTF-8"><title>flask_WTF</title> </head> <body> <form method="post"><label>用戶名:</label><input type="text" name="username"><br><label>密碼:</label><input type="password" name="password"><br><label>確認(rèn)密碼:</label><input type="password" name="password2"><br><input type="submit" value="提交"><br> </form> </body> </html>
flash消息閃現(xiàn),給模板傳遞消息,消息加密(做混淆用)
flask_WTF.py
# -*- coding: utf-8 -*- """ @File : flask_WTF.py @Time : 2021/1/27 14:10 @Author : Dontla @Email : sxana@qq.com @Software: PyCharm """ from flask import Flask, render_template, request, flashapp = Flask(__name__) app.secret_key = '12345678' # 隨便填@app.route('/', methods=["GET", "POST"]) def index():if request.method == 'POST':username = request.form.get('username')password = request.form.get('password')password2 = request.form.get('password2')if not all([username, password, password2]):# return '參數(shù)不完整'flash('參數(shù)不完整')else:# return 'success'return 'sucess'return render_template('index_WTF.html')if __name__ == '__main__':app.run(debug=True)index_WTF.html
<!DOCTYPE html> <html lang="en"> <head><meta charset="UTF-8"><title>flask_WTF</title> </head> <body> <form method="post"><label>用戶名:</label><input type="text" name="username"><br><label>密碼:</label><input type="password" name="password"><br><label>確認(rèn)密碼:</label><input type="password" name="password2"><br><input type="submit" value="提交"><br>{% for message in get_flashed_messages() %}{{ message }}{% endfor %} </form> </body> </html>
表單樣式的顯示(使用flask_wtf模板創(chuàng)建表單)
https://www.bilibili.com/video/BV17W41177oE?p=16
# -*- coding: utf-8 -*- """ @File : flask_WTF.py @Time : 2021/1/27 14:10 @Author : Dontla @Email : sxana@qq.com @Software: PyCharm """ from flask import Flask, render_template, request, flash from flask_wtf import FlaskForm from wtforms import SubmitField, PasswordField, StringFieldapp = Flask(__name__) app.secret_key = '12345678' # 隨便填# 自定義表單類 class LoginForm(FlaskForm):username = StringField('用戶名:')password = PasswordField('密碼:')password2 = PasswordField('確認(rèn)密碼:')submit = SubmitField('提交')@app.route('/form', methods=['GET', 'POST']) def login():login_form = LoginForm()# 1. 判斷請(qǐng)求方式if request.method == 'POST':# 2. 獲取請(qǐng)求的參數(shù)username = request.form.get('username')password = request.form.get('password')password2 = request.form.get('password2')# 3. 驗(yàn)證參數(shù). WTF可以一句話就實(shí)現(xiàn)所有的校驗(yàn)# 我們沒有CSRF tokenif login_form.validate_on_submit():print(username, password)return 'success'else:flash('參數(shù)有誤')return render_template('index_WTF.html', form=login_form)@app.route('/', methods=["GET", "POST"]) def index():if request.method == 'POST':username = request.form.get('username')password = request.form.get('password')password2 = request.form.get('password2')if not all([username, password, password2]):# return '參數(shù)不完整'flash('參數(shù)不完整')else:# return 'success'return 'sucess'return render_template('index_WTF.html')if __name__ == '__main__':app.run(debug=True)index_WTF.html
<!DOCTYPE html> <html lang="en"> <head><meta charset="UTF-8"><title>flask_WTF</title> </head> <body> <form method="post"><label>用戶名:</label><input type="text" name="username"><br><label>密碼:</label><input type="password" name="password"><br><label>確認(rèn)密碼:</label><input type="password" name="password2"><br><input type="submit" value="提交"><br>{% for message in get_flashed_messages() %}{{ message }}{% endfor %} </form><hr><form method="post">{{ form.csrf_token() }}{{ form.username.label }}{{ form.username }} <br>{{ form.password.label }}{{ form.password }} <br>{{ form.password2.label }}{{ form.password2 }} <br>{{ form.submit}} <br> </form> </body> </html>模板表單的驗(yàn)證 csrf_token
常用驗(yàn)證函數(shù)
怎么使用?
老師說validators是復(fù)數(shù),所以可以傳個(gè)列表,這推導(dǎo)??
但這寫個(gè)tuple是啥意思?反正就是可迭代對(duì)象就行?(不能用tuple,會(huì)報(bào)錯(cuò),只能用列表)
Flask中使用SQL數(shù)據(jù)庫 SQLAlchemy(flask_sqlalchemy)(如果是使用mysql數(shù)據(jù)庫,安裝flask_mysqldb)
在電腦上還要?jiǎng)?chuàng)建sql數(shù)據(jù)庫??
裝好了,按照這個(gè)安裝就行
數(shù)據(jù)庫mysql是什么?win10如何安裝使用mysql?
然后控制臺(tái)執(zhí)行命令,登入數(shù)據(jù)庫:
C:\Users\Dontla>mysql -u root -p Enter password: **** Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 12 Server version: 8.0.23 MySQL Community Server - GPLCopyright (c) 2000, 2021, Oracle and/or its affiliates.Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners.Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.mysql>創(chuàng)建數(shù)據(jù)庫:
mysql> create database flask_demo charset=utf8; Query OK, 1 row affected, 1 warning (0.02 sec)mysql>使用數(shù)據(jù)庫:
mysql> use flask_sql_demo; Database changed mysql>顯示數(shù)據(jù)庫表單:
mysql> show tables; Empty set (0.01 sec)mysql>mysql數(shù)據(jù)模型的定義 常用SQLAlchemy字段類型/列選項(xiàng)/關(guān)系選項(xiàng)
表格式:
兩個(gè)表做兩個(gè)class
代碼看下一節(jié)
數(shù)據(jù)庫基本操作,增刪改 db.session
我勒個(gè)去,應(yīng)該找個(gè)python3的教程的,這個(gè)python2很多庫用法不支持,替換也替換不了。。。。https://www.bilibili.com/video/BV17W41177oE?p=20&spm_id_from=pageDriver到此為止,再見!
總結(jié)
以上是生活随笔為你收集整理的Flask学习 黑马程序员-6节课入门Flask框架web开发视频(中途撤退,寻找py3教程)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: python 虚拟环境 virtuale
- 下一篇: vscode怎么用命令行打开本地项目(c