From f85141cccda822025ecbb3278df6ade239766ec5 Mon Sep 17 00:00:00 2001 From: giteeyunyunyun <18883994582@163.com> Date: Thu, 25 Mar 2021 17:59:30 +0800 Subject: [PATCH 1/2] 13-1note --- ...5\347\273\223\346\236\204-13-1-noteyun.md" | 149 ++++++++++++++++++ 1 file changed, 149 insertions(+) create mode 100644 "\347\254\254\344\272\214\346\234\237\350\256\255\347\273\203\350\220\245/5\347\217\255/5\347\217\255_\344\272\221/week13/django\345\256\211\350\243\205\343\200\201\347\233\256\345\275\225\347\273\223\346\236\204-13-1-noteyun.md" diff --git "a/\347\254\254\344\272\214\346\234\237\350\256\255\347\273\203\350\220\245/5\347\217\255/5\347\217\255_\344\272\221/week13/django\345\256\211\350\243\205\343\200\201\347\233\256\345\275\225\347\273\223\346\236\204-13-1-noteyun.md" "b/\347\254\254\344\272\214\346\234\237\350\256\255\347\273\203\350\220\245/5\347\217\255/5\347\217\255_\344\272\221/week13/django\345\256\211\350\243\205\343\200\201\347\233\256\345\275\225\347\273\223\346\236\204-13-1-noteyun.md" new file mode 100644 index 00000000..da1f1ca0 --- /dev/null +++ "b/\347\254\254\344\272\214\346\234\237\350\256\255\347\273\203\350\220\245/5\347\217\255/5\347\217\255_\344\272\221/week13/django\345\256\211\350\243\205\343\200\201\347\233\256\345\275\225\347\273\223\346\236\204-13-1-noteyun.md" @@ -0,0 +1,149 @@ +# 13-1-noteyun + +https://docs.djangoproject.com/zh-hans/2.0/ + +``` +https://docs.djangoproject.com/zh-hans/2.0/ +``` + +# 安装Django + +``` +pip install django==2.0 + +# 下载比较慢的同学可以换个源下载 +pip install django==2.0 -i https://pypi.tuna.tsinghua.edu.cn/simple +``` + +# 创建项目 + +``` +# 创建虚拟环境 +cd mysite +python -m venv venv + +>python manage.py --help + +``` + +# 目录结构 + +``` +mysite/ + manage.py + mysite/ + __init__.py + settings.py + urls.py + wsgi.py +``` + +- `manage.py` + + 管理`django`项目的**命令行工具**, `manage.py`就是`django-admin`, 只是他们有以下区别 + + - `django-admin`会被加入到环境变量当中, 所以我们可以在任意目录下调用. + - `manage.py`只能**在该项目下使用.** + +- `mysite`(项目名) + + 实际业务逻辑编写, 项目配置, 路由管理的地方. + + - `__init__.py` + + 让`Python`将当前目录识别为一个包. + + - `settings.py` + + 项目的配置文件. + + - `wsgi.py` + + `web server gateway interface` **web服务网关接口.** + + `wsgi`只是一个协议, 它确立了两个规范: + + - 我们可以在服务器下开启多个web服务应用, 确定客户端请求如何到达**服务应用**的规范. + - **服务应用**如何把处理的结果返回. + + - urls.py + + 网站的目录,url调度器; + + + + + +# 启动项目 + +- 创建一个应用 + + 创建的应用才是真正处理请求和编写业务逻辑的地方. + + ``` + python manage.py startapp polls + ``` + +- 启动项目 + + ``` + # 5000为指定的端口, 不指定端口默认为8000 + python manage.py runserver 5000 + python manage.py runserver + + ``` + + ctrl+c 即可关闭 + + + +# MTV模型## + +![img](https://i.im5i.com/2021/01/13/MTV.png) + +- Model + + `django`提供了一个抽象的模型层, 目的是为了构建和操纵当前web应用的数据. + + > 简单地说, model层就是用来和数据库进行交互的. + > + > django最出名的就是它的orm + + - orm + + `object relation mapping` **对象关系映射** + + orm自动地根据对象在数据库中进行操作. orm使得我们不用去操心数据表的建立和修改, 数据的CRUD, 只关心逻辑层的处理 + +- Template + + **View将数据传递给模板层进行渲染,** 之后将渲染好的页面返回到客户端. + + 模板层提供了一个对设计者友好的语法**用于渲染向用户呈现的信息.** + + > 类似PPT模板 + +- View + + 视图层, 负责处理用户的请求并返回响应. + +# 课后作业 + +- 安装django和创建django项目 +- 理解MTV模型. + +![image-20210324104558205](C:\Users\yunqin\AppData\Roaming\Typora\typora-user-images\image-20210324104558205.png) + + + +![image-20210324104803778](C:\Users\yunqin\AppData\Roaming\Typora\typora-user-images\image-20210324104803778.png) + + + +image-20210324104848297![image-20210324105027046](C:\Users\yunqin\AppData\Roaming\Typora\typora-user-images\image-20210324105027046.png) + +![image-20210324105027046](C:\Users\yunqin\AppData\Roaming\Typora\typora-user-images\image-20210324105027046.png) + + + +image-20210324105106646 \ No newline at end of file -- Gitee From 58f6d747f300176c781a8d0379ac81ee856558b4 Mon Sep 17 00:00:00 2001 From: giteeyunyunyun <18883994582@163.com> Date: Fri, 26 Mar 2021 12:59:35 +0800 Subject: [PATCH 2/2] 13-2uzoye --- .../week13/django-13-2-noteyun.md" | 182 ++++++++++++++++++ 1 file changed, 182 insertions(+) create mode 100644 "\347\254\254\344\272\214\346\234\237\350\256\255\347\273\203\350\220\245/5\347\217\255/5\347\217\255_\344\272\221/week13/django-13-2-noteyun.md" diff --git "a/\347\254\254\344\272\214\346\234\237\350\256\255\347\273\203\350\220\245/5\347\217\255/5\347\217\255_\344\272\221/week13/django-13-2-noteyun.md" "b/\347\254\254\344\272\214\346\234\237\350\256\255\347\273\203\350\220\245/5\347\217\255/5\347\217\255_\344\272\221/week13/django-13-2-noteyun.md" new file mode 100644 index 00000000..0736caa4 --- /dev/null +++ "b/\347\254\254\344\272\214\346\234\237\350\256\255\347\273\203\350\220\245/5\347\217\255/5\347\217\255_\344\272\221/week13/django-13-2-noteyun.md" @@ -0,0 +1,182 @@ +13-2-noteyun + +https://docs.djangoproject.com/zh-hans/2.0/intro/tutorial01/ + +``` +https://docs.djangoproject.com/zh-hans/2.0/intro/tutorial01/ +``` + +# 创建视图 + +编写视图polls/views.py + +不同请求调用不同的处理方法,路由绑定;(该应用的路由+全局的路由) + + + + + +- 视图就是处理客户端请求的地方`views.py` + + ``` + def index(request): + """ + request 就是作为参数传递进来的请求对象 + :param request: + :return: HttpResponse 处理完请求的返回对象 + """ + return HttpResponse("Hello world. You're at the polls index.") + ``` + +- 解决不同请求调用不同的处理方法, 需要通过路由 + + - 创建应用的路径管理 + + ``` + from django.urls import path + from . import views + + urlpatterns = [ + path('', views.index, name='index'), + ] + ``` + + - 将应用的路径管理添加到全局配置当中 + + ``` + from django.urls import path, include + + urlpatterns = [ + path('polls/', include("polls.urls")), + ] + ``` + + + +# 创建模型 + + + +- 数据库配置`settings.py` + + ``` + #原来 + + DATABASES = { + 'default': { + 'ENGINE': 'django.db.backends.sqlite3', + 'NAME': os.path.join(BASE_DIR, 'db.sqlite3'), + } + } + ``` + + + + ``` + DATABASES = { + 'default': { + 'ENGINE': 'django.db.backends.mysql', + 'NAME': 'polls', + 'USER': 'root', + 'PASSWORD': 'qwe369', + 'HOST': '127.0.0.1', + 'PORT': '3306' + } + } + ``` + + - 修改连接数据库的客户端`mysite\__init__.py` + + ``` + import pymysql + + pymysql.install_as_MySQLdb() + ``` + +- 初始化数据库 + + - 新建数据库`polls` + + image-20210326114125930![image-20210326114346946](C:\Users\yunqin\AppData\Roaming\Typora\typora-user-images\image-20210326114346946.png) + + + + - 完成第一次迁移, 生成初始的管理表(见上图) + + ``` + python manage.py migrate + ``` + +- 创建模型 + + ```python + from django.db import models + + # Create your models here. + + + class Question(models.Model): + question_text = models.CharField(max_length=200) + pub_data = models.DateTimeField('date published') + + + class Choice(models.Model): + question = models.ForeignKey(Question, on_delete=models.CASCADE)#跟着它一起删除 + choice_text = models.CharField(max_length=200) + votes = models.IntegerField(default=0) + ``` + +- 激活模型 + + - 激活APP`settings.py` + + ```python + INSTALLED_APPS = [ + 'polls.apps.PollsConfig',#激活 + 'django.contrib.admin', + 'django.contrib.auth', + ... + ] + ``` + + - 同步本地模型修改(**类似git commit)** + + ``` + python manage.py makemigrations 应用名 + ``` + + - 会在当前应用`migrations`目录下生成相应的记录文件 + + - 正式进行数据库同步 + + ``` + python manage.py migrate + ``` + +# 管理后台 + +- 创建管理员(密码:8!)8位 + + ``` + python manage.py createsuperuser + ``` + +- 向管理页面加入投票应用 polls/admin.py + + ``` + from django.contrib import admin + + # Register your models here. + from .models import Question + + admin.site.register(Question) + ``` + +# 课后作业 + +- 完成视图, 模型, 管理后台模块的编写 + +![image-20210326125647685](C:\Users\yunqin\AppData\Roaming\Typora\typora-user-images\image-20210326125647685.png) + + + -- Gitee