password
URL
type
status
date
slug
summary
tags
category
icon
标签
是否汉化
人生百味,体验过后,淡然处之也是一种姿态。
项目Tree
~/Desktop/django_train/jobs tree -L 3
tree
.
├── init.py
├── pycache
│ ├── init.cpython-38.pyc
│ ├── admin.cpython-38.pyc
│ ├── apps.cpython-38.pyc
│ ├── models.cpython-38.pyc
│ ├── urls.cpython-38.pyc
│ └── views.cpython-38.pyc
├── admin.py
├── apps.py
├── migrations
│ ├── 0001_initial.py
│ ├── init.py
│ └── pycache
│ ├── 0001_initial.cpython-38.pyc
│ └── init.cpython-38.pyc
├── models.py
├── templates
│ ├── base.html
│ ├── jobdetail.html
│ └── joblist.html
├── tests.py
├── urls.py
└── views.py
定义详情页
添加jobdetail.html
定义详情页视图
添加详情页视图函数
Remark-两种写法的区别:
return HttpResponse(template.render(context))
return render(request, 'jobdetail.html', context)
在 Django 中,这两种写法都可以用来渲染模板并返回响应,主要区别在于:
- HttpResponse():
- 更底层的写法,需要直接导入HttpResponse。
- 需要自己将context渲染到模板,得到渲染后的内容。
- 需要自己设置响应的内容类型和编码。
示例:
- render():
- 更高层的写法,直接由框架处理模板渲染和响应。
- 将context和模板名传给render,它会帮你渲染模板。
- 会自动设置Content-Type和编码。
示例
综上,render()写法更简洁,使用起来也更方便,因此大多数情况下优先推荐使用render()。
HttpResponse()适用于需要更底层控制的场景。
应用jobs:将jobdetail视图注册到urls.py中
访问详情页
- 点击职位名称,跳转至详情页 http://127.0.0.1:8080/jobdetail/1/
![notion image](https://www.notion.so/image/https%3A%2F%2Fprod-files-secure.s3.us-west-2.amazonaws.com%2F68202bb5-9faf-4bfe-9cc4-aaf29bc1fbcc%2Fe963ee89-9a8f-4039-9796-a44bc3c514f8%2FUntitled.png?table=block&id=5cdfed93-9e9a-4e65-ae18-8aa1105c0fed&t=5cdfed93-9e9a-4e65-ae18-8aa1105c0fed&width=528&cache=v2)
👇🏻参考资料
离开乏味的皮囊,自由的灵魂在路上
- Name: Alan Hsu
- Tag: 随感、技术、经验、旅行、推荐、生活、音乐、电影 etc.
- Email:xulanzhong521gmail.com
- Twitter:@AlanHsu521
- Telegram:@AlanHsu521
- WeChat: Alan_Hsu_521
- Github: https://github.com/xsoway/
![notion image](https://cdn.jsdelivr.net/gh/xsoway/xmylog_imgs@main/wechat.jpeg?t=a3e6e2a6-177a-47aa-838a-5a5e3fadf33f)
![notion image](https://cdn.jsdelivr.net/gh/xsoway/xmylog_imgs@main/%E5%85%AC%E4%BC%97%E5%8F%B7.jpeg?t=8cb23529-b12c-4559-aca9-c49f174e9145)
- Author:Alan_Hsu
- URL:https://xmylog.com/article/articles_django-3
- Copyright:All articles in this blog, except for special statements, adopt BY-NC-SA agreement. Please indicate the source!
Relate Posts