前端開發
目的: 開發一個平臺(網站)
- 前端開發:HTML CSS JavaScript
- web框架:接受請求和處理
- MySQL數據庫:存儲數據的地方快速上手:基于Flask Web框架快速搭建一個網站
深度學習:基于Django框架(主要)
1.快速開發網站
pip install flask
from flask import Flaskapp = Flask(__name__)# 創建了網址show/info和函數 index的對應關系
# 以后用戶在瀏覽器上訪問show/info,網站會自動執行 index@app.route("/show/info")
def index():return "中國聯通"
if __name__ == '__main__':app.run()
網站與別人不一樣:
瀏覽器可以識別多標簽+數據 例如:<h1>中國<h1> ->瀏覽器看見加大加粗<span style='color:red,'>
- Flask框架為了讓我們寫標簽方便,支持將字符串(網頁返回的本質是一個字符串)寫在文件里面
from flask import Flask ,render_templateapp = Flask(__name__)# 創建了網址show/info和函數 index的對應關系
# 以后用戶在瀏覽器上訪問show/info,網站會自動執行 index@app.route("/show/info")
def index():#Flask內部會自動打開文件并讀取,將內容給用戶返回#默認去templates文件夾中去尋找(需要自己添加,并創建html文件)return render_template("index.html")
if __name__ == '__main__':app.run()
2.瀏覽器能識別的標簽
2.1 編碼(head)
<meta charset="UTF-8">
2.2 title(head)
<head><meta charset="UTF-8"><title>Title</title>
</head>
2.3 標題
<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><title>學習web</title>
</head>
<body><h1>1級標題</h1><h2>2級標題</h2><h3>3級標題</h3><h4>4級標題</h4><h5>5級標題</h5><h6>6級標題</h6>
</body>
</html>
2.4 div和span
<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><title>學習web</title>
</head>
<body><div>哈哈哈</div><div>啦啦啦啦啦</div>
</body>
</html>
-
div :一個人占一整行【塊級標題】
<div>哈哈哈</div>
-
span :自己多大就占多少【行內標簽、內聯標簽】
<span>哈哈哈</span>
注意:這兩個標簽比較素+CSS
2.5 超鏈接
跳轉到其他網站(絕對路徑)
<a href="https://www.csdn.net/">點擊跳轉</a>
跳轉到自己的網站(相對路徑)
<a href="http://127.0.0.1:5000/show/info">點擊跳轉</a>
簡化:<a href="/show/info">點擊跳轉</a>
2.6 圖片
<img src="圖片地址"/>
<img src="自己圖片地址"/>
顯示自己圖片:-自己的項目中創建:static目錄,圖片要放在里面-頁面上引用圖片 <img src="/static/圖片名字"/>
顯示別人的圖片(防盜鏈)
<img src="圖片地址"/>
設置圖片的高度和寬度
<img style="height:100px;width:100px" src="圖片地址"/>
<img style="height:10%;width:10%" src="圖片地址"/>
小結
-
學習的標簽
<h1></h1> <div></div> <span></span> <a></a> <img />
-
劃分
- 塊級標簽<h1></h1><div></div> - 行內標簽(緊挨著放在一起)<span></span><a></a><img />
-
嵌套
<div><span>xxx</span><img /><a></a> </div>
案例:商品列表
<!DOCTYPE html> <html lang="en"> <head><meta charset="UTF-8"><title>Title</title> </head> <body><h1>商品名稱</h1><a href="https://www.zhihu.com/" target="_blank"><img src="/static/a2.jpg" style="height:150px"></a><a href="https://www.zhihu.com/" target="_blank"><img src="/static/a1.jpg" style="height:150px"></a><a href="https://www.zhihu.com/" target="_blank"><img src="/static/a3.jpg" style="height:150px"></a> </body> </html>
2.7 列表
<ul><li>中國移動</li><li>中國聯通</li><li>中國電信</li>
</ul>
<ol><li>中國移動</li><li>中國聯通</li><li>中國電信</li>
</ol>
2.8 表格
<table border="1">(加邊框)<thead><tr> <th>ID</th> <th>姓名</th> <th>年齡</th> </tr></thead><tbody><tr> <td>1</th> <th>hh</th> <th>59</th> </tr><tr> <th>2</th> <th>dd</th> <th>9</th> </tr><tr> <th>3</th> <th>ww</th> <th>5</th> </tr><tr> <th>4</th> <th>ee</th> <th>1</th> </tr><tr> <th>5</th> <th>rr</th> <th>3</th> </tr><tr> <th>6</th> <th>tt</th> <th>4</th> </tr></tbody>
</table>
案例:用戶列表
<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><title>Title</title>
</head>
<body>
<h1>用戶列表</h1>
<table border="1"><thead><tr><th>ID</th><th>頭像</th><th>姓名</th><th>郵箱</th><th>更多信息</th><th>操作</th></tr></thead><tbody><tr><td>1</td><td><img src="/static/a1.jpg" style="height:50px"></td><td>wcx</td><td>123456@qq.com</td><td><a href="https://www.zhihu.com/">查看詳細</a></td><td>編輯 刪除</td></tr><tr><td>1</td><td><img src="/static/a1.jpg" style="height:50px"></td><td>wcx</td><td>123456@qq.com</td><td><a href="https://www.zhihu.com/">查看詳細</a></td><td>編輯 刪除</td></tr><tr><td>1</td><td><img src="/static/a1.jpg" style="height:50px"></td><td>wcx</td><td>123456@qq.com</td><td><a href="https://www.zhihu.com/">查看詳細</a></td><td>編輯 刪除</td></tr></tbody>
</table></body>
</html>
2.9 input系列(7個)
<input type="text">
<input type="password">
<input type="file">
<input type="radio" name="n1">男
<input type="radio" name="n1">女
<input type="checkbox" >籃球
<input type="checkbox" >乒乓球
<input type="checkbox" >網球
<input type="checkbox" >羽毛球
<input type="button" value="提交"> -->普通的按鈕
<input type="submit" value="提交"> -->提交表單
2.10 下拉框
<select><option>北京</option><option>上海</option><option>深圳</option>
</select>
<select multiple>(shift多選)<option>北京</option><option>上海</option><option>深圳</option>
</select>
2.11 多行文本
<textarea></textarea>
案例:用戶注冊
<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><title>Title</title>
</head>
<body>
<h1>用戶注冊</h1>
<div>用戶名: <input type="text">
</div>
<div>密碼: <input type="password">
</div>
<div>性別: <input type="radio" name="n1">男<input type="radio" name="n1">女
</div>
<div>愛好: <input type="checkbox">籃球<input type="checkbox">乒乓球<input type="checkbox">羽毛球
</div>
<div>城市:<select ><option >北京</option><option >上海</option><option >重慶</option></select>
</div>
<div>備注:<textarea></textarea>
</div>
<div><input type="button" value="提交">
</div>
</body>
</html>
知識點回顧和補充
-
網站請求流程
-
一大堆標簽
h/div/span/a/img/ul/li/table/input/textarea/select
3.網絡請求
-
在瀏覽器的URL中輸入地址,點擊回車,訪問。
瀏覽器會發送數據過去,本質上發送的是字符串: "GET/explore httpxxxxxxxxxxxxxxxxx" "POST/explore httpxxxxxxxxxxxxxxxxxxxxxxx"
-
瀏覽器向后端發送請求
-
GET請求【URL訪問/表單提交】
-
現象:跳轉、向后臺傳入數據會拼接在URL上
https://www.sogou.com/web?query=%E5%AE%89%E5%8D%93&_ast=1754632236&_asf=www.sogou.com
注意:GET請求的數據會在URL上體現
-
-
POST請求【表單提交】
- 現象:提交數據不在URL中而是在請求體中
-
案例:用戶注冊
- 新創建項目
- 創建Flask代碼
from flask import Flask, render_template, requestapp = Flask(__name__)@app.route("/register",methods=["GET","POST"])
def register():if request.method== 'GET':return render_template("register.html")else:user = request.form.get("user")pwd = request.form.get("pwd")gender = request.form.get("gender")hobby_list = request.form.get("hobby")city = request.form.get("city")more = request.form.get("more")return "注冊成功"if __name__ == '__main__':app.run()
- html代碼
<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><title>Title</title>
</head>
<body>
<h1>用戶注冊</h1>
<form method="post" action="/register" >
<div>用戶名: <input type="text" name="user"></div><div>密碼: <input type="password" name="pwd"></div><div>性別: <input type="radio" name="gender" value="1">男<input type="radio" name="gender" value="2">女</div><div>愛好: <input type="checkbox" name="hobby" value="20">籃球<input type="checkbox" name="hobby" value="30">乒乓球<input type="checkbox" name="hobby" value="40">羽毛球
</div><div>城市:<select name="city"><option value="122">北京</option><option value="123">上海</option><option value="124">重慶</option></select>
</div><didv>備注:<textaera name="more"></textaera></didv><input type="submit" value="submit提交">
</form></body>
</html>
- form標簽包裹要提交的數據
- 提交方式:method=“”
- 提交地址:action=“”
- 在form標簽里面必須有一個submit
- 在form里面的一些標簽:input/select/textarea
- 一定要寫name屬性
<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><title>Title</title>
</head>
<body>
<h1>用戶注冊</h1>
<div><form method="post" action="/post/reg" >用戶名: <input type="text" name="user">密碼: <input type="password" name="pwd"><input type="button" value="button提交"><input type="submit" value="submit提交"></form>
</div>
</body>
</html>
總結
1.稱呼
-HTML標簽(超文本傳輸語言)與瀏覽器搭配
2.HTML標簽(默認格式樣式,可通過手段進行修改)
3.HTML標簽與編程語言無關
4.提醒:HTML標簽比較多,標簽還有很多,不必逐一學會