django新手村4 -----templates

再来说说templates

先修改在noob1 中的setting.py,找到TEMPLATES,修改DIR
https://ithelp.ithome.com.tw/upload/images/20211225/20135895nWZ4DWqWjl.jpg

跟目录创建templates 资料夹,再创建student_list.html在里面
https://ithelp.ithome.com.tw/upload/images/20211225/20135895F0T0Y2Sftl.jpg

回想一下前面提到的 主urls->次urls->views->models->views->template

主urls之前已经将次urls include进来,所以可以直接从次urls.py开始修改
https://ithelp.ithome.com.tw/upload/images/20211225/20135895RTBPxqrZmn.jpg

修改app 中的urls.py
https://ithelp.ithome.com.tw/upload/images/20211225/20135895AZLZfQIJkS.jpg

修改app 中的views.py
https://ithelp.ithome.com.tw/upload/images/20211225/20135895LitvJEMiWa.jpg
views向models请求资料,并回传,这就是views->models->views

修改templates 资料夹里的 student_list.html
https://ithelp.ithome.com.tw/upload/images/20211225/201358956SLWURlqX9.jpg

网址输入 http://127.0.0.1:8000/app/student_list/ 就可以看到学生名字了

补充一些小东西

views.py 中
捞取所有在Student资料库里的资料
students = Student.objects.all()
传递物件或值的时候必须使用字典型态
context = {
'students': students
}
这一行其实本质上和之前的HttpResponse是一样的,使用Pycharm的人可以滑鼠点到render在案ctrl+b,看更多细节
return render(request, 'student_list.html', context=context)
也可以写成这样,locals()会自动的将所以变数传入
return render(request, 'student_list.html', context=locals())

student_list.html
编辑html时快捷键:
惊叹号,再按tab (会出现基本的要件)
tag标签,再按tab (会补齐前後<>)
可以用*创建多个 (li星星5,再按tab)

在html使用来自後端传来的值:
{{}} 两个{}的表示直接调用变数
{%%} 表示要使用已定义的关键字,那要记得跟逻辑相关的都要end,有for就要有endfor,有if就要有endif


<<:  STM32开发笔记02---新建库函数工程

>>:  27.MYSQL 资料统整1

Spring Framework X Kotlin Day 24 Performance Test

GitHub Repo https://github.com/b2etw/Spring-Kotlin...

LeetCode解题 Day01

565. Array Nesting https://leetcode.com/problems/a...

Vue Components 子元件之间的资料传递

沟通不良的代价就是彼此越来越疏远,然後行同陌路如陌生人。 子元件之间就像兄弟一样的关系,所以这篇要...

EP 26: MockData come back by (a little bit) DI design

Hello, 各位 iT邦帮忙 的粉丝们大家好~~~ 本篇是 Re: 从零开始用 Xamarin 技...

PERT 图

计画评核术 PERT (Program Evaluation and Review Techniqu...