From 88c783c51d846a493f9428ed8ad2c6d2ab37a47d Mon Sep 17 00:00:00 2001 From: giteeyunyunyun <18883994582@163.com> Date: Sun, 28 Mar 2021 23:49:47 +0800 Subject: [PATCH] 13-3zuoye --- ...o\350\247\206\345\233\276-13-3-noteyun.md" | 96 +++++++++++++++++++ 1 file changed, 96 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\350\247\206\345\233\276-13-3-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\350\247\206\345\233\276-13-3-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\350\247\206\345\233\276-13-3-noteyun.md" new file mode 100644 index 00000000..6d728fb3 --- /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\350\247\206\345\233\276-13-3-noteyun.md" @@ -0,0 +1,96 @@ +13-3-noteyun + +https://docs.djangoproject.com/zh-hans/2.0/ + +https://docs.djangoproject.com/zh-hans/2.0/intro/tutorial04/ + + + +``` +https://docs.djangoproject.com/zh-hans/2.0/ + +E:\PycharmProjects\Spider_Code\mysite + +(py37b) E:\PycharmProjects\Spider_Code\mysite> +``` + + + +> 遇到修改后模板文件没有变化的情况, 要想到去清除浏览器缓存. + +image-20210328131226833 + +# 视图层返回类型 + +- 简单文本 +- 文件 + - html + - css + - js + - 视频文件, 音频文件 +- JSON数据格式 + +# 路由配置 + +https://docs.djangoproject.com/zh-hans/2.0/intro/tutorial03/ + +- URL匹配 + + ``` + path('/', views.detail, name='detail'), + ``` + + - int表示类型 + - question_id表示传递的参数名 + +- URL配置 + + - ROOT_URLCONF + + 设置主URL配置文件. + +# 模板 + +``` +def index(request): + """ + request 就是作为参数传递进来的请求对象 + :param request: + :return: HttpResponse 处理完请求的返回对象 + """ + latest_question_list = Question.objects.order_by('-pub_date')[:5] + context = { + 'latest_question_list': latest_question_list, + } + return render(request, 'polls/index.html', context) +``` + +# 重定向 + +- 当需要对用户不同的操作做不同结果的渲染时, 需要用到重定向. 可以简单理解为当前网址自动帮你跳转到另外一个网址. + + ``` + return HttpResponseRedirect(reverse('polls:results', args=(question.id,))) + ``` + +# 通用视图 generic view + +通用视图将常见的模式抽象化,可以使你在编写应用时甚至不需要编写Python代码。 + +> 通用视图也是未来我们学习restful api的雏形. + +# 课后作业 + +- 完成polls投票应用 + +- 了解和使用通用视图 + +- 预习orm的shell操作. + + ``` + https://docs.djangoproject.com/zh-hans/2.0/intro/tutorial02/ + 初试API + ``` + + + -- Gitee