Files
DailyChallenge_ProgrammingD…/Day_14/README.md
T

67 lines
2.5 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# Day_14 · pip 安装 Django,认识 HTTP
> 预计耗时:25~30 分钟
> 你马上要接触第一个"框架"了。今天两件事:用 pip 装上 Django,并搞懂网页是怎么请求数据的。
## 今日知识点
- **pip**:Python 的"应用商店",`pip install 包名` 装第三方库
- **Django**:别人写好的 Web 框架,你基于它写后端
- **HTTP 请求/响应**:浏览器(客户端)发请求 → 服务器处理 → 返回响应(网页/数据)
- **URL 结构**:`https://www.runoob.com/python3/python3-tutorial.html`
= 协议(`https`) + 域名(`www.runoob.com`) + 路径(`/python3/python3-tutorial.html`)
## 先学(教程)
- 菜鸟教程 Django(先看"简介"和"环境安装"部分):https://www.runoob.com/django/django-tutorial.html
- B站 Django(看前 1~2 讲:django 安装 / 创建项目):https://www.bilibili.com/video/BV1L8bpeXET5/
## 任务(后做)
1. 命令行安装 Django:
```
pip install django
```
2. 验证安装成功:
```
python -c "import django; print(django.get_version())"
```
看到版本号(如 `5.x.x`)即成功。报"ModuleNotFoundError"就说明没装上,重新执行第 1 步。
3. 认识 HTTP 与 URL:
- 用浏览器打开 https://www.runoob.com/python3/python3-tutorial.html
- 观察地址栏,指认:哪部分是协议?哪部分是域名?哪部分是路径?
- 在页面上按 F12 → 切换到"网络/Network"标签 → 刷新页面,
能看到一列"请求"记录——这就是浏览器向服务器发的 HTTP 请求。
4. **进阶(加分)**:用 Python 发一个 HTTP 请求(`urllib` 是内置模块,不用安装):
```python
from urllib.request import urlopen
resp = urlopen("https://www.runoob.com/python3/python3-tutorial.html")
print(resp.status) # 200 表示成功
print(resp.read()[:100]) # 返回的内容(前 100 字节)
```
感受一下:你的电脑作为"客户端"去请求了一个"服务器"。
## 完成标准
- [ ] 版本号打印成功
- [ ] 能指认 URL 的三部分
- [ ] (进阶)Python 请求返回 200
**提交方式**:把命令行安装 + 版本号的**截图**贴在当日工单评论里。
## 参考解答(先做再看)
```python
# 命令行三步:
# pip install django
# python -c "import django; print(django.get_version())"
# 看到 5.x.x 即成功
from urllib.request import urlopen
resp = urlopen("https://www.runoob.com/python3/python3-tutorial.html")
print(resp.status) # 200
print(resp.read()[:100])
```