Django自学笔记-第二章
内容来源: 极客时间
创建应用
1 | cd recuritment |
观察应用文件情况
| 文件名 | 用途 | 说明 |
|---|---|---|
| admin.py | 管理控制文件 | |
| apps.py | 应用相关文件 | |
| models.py | 用户定义的模型 | 主要模型定义文件 |
| tests.py | 测试文件 | |
| views.py | 用户定义的视图文件 |
创建自定义脚本
1 | from django.db import models |
其中对于models的字段属性有如下介绍
| 字段类型 | 字段名称 | 说明 |
|---|---|---|
| SmallIntegerField | 数字类型 | 单选框 |
| CharField | 字符类型 | 短字符串 |
| TextField | 字符类型 | 长字符串 |
| ForeignKey | 外键引用类型 | 引用外部属性 |
| DateTimeField | 时间类型 | 展示时间 |
对于User的外键引用, 需要先导入操作:
from django.contrib.auth.models import User
添加后访问页面, 发现新添加的Job并未正确显示
添加自定义应用到管理页面
添加下面信息到Jobs/admin.py中
1 | from django.contrib import admin |
在recuritment/settings.py的INSTALLED_APPS列表中追加jobs应用
此时刷新页面可以观察到Jobs应用已显示
但是如果点击进去会报错
因为并未进行数据库表同步操作
执行迁移同步操作
1 | ./manage.py makemigrations |
执行./manage.py runserver 0.0.0.0:9999后登录页面发现可以管理Jobs应用
新增职位
点击右侧新增JOB+, 添加招聘信息, 如下图
目前存在不友好点:
- 创建人应自动填充为当前用户
- 创建时间应自动填充为当前时间
- 修改时间应根据修改后的时间自动调整
点击右下角保存
发现仍然存在不足,
如Job object(1)应展示为职位的中文名称
完善产品逻辑
完善时间字段-设置default参数
通过指定default参数来指定默认值, 参见以下代码
1 | from datetime import datetime |
完善创建人字段及职位展示字段-覆写ModleAdmin类
通过在app的admin.py中,
覆写父类ModleAdmin并注册完成对应用的优化
1 | class JobAdmin(admin.ModelAdmin): |
结果展示
新增职位展示页面
构造骨架页面
添加
jobs/templates/base.html文件1
2
3
4
5
6
7
8<!-- base.html -->
<h1 style="margin: auto;width: 50%;">金风科技开放职位</h1>
<p>
</p>
{% block content %}
{% endblock %}构造职位列表页面
添加
jobs/templates/joblist.html文件1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18{% extends 'base.html' %}
{% block content %}
终于等到你, 期待你的加入, 用技术去探索新世界!
{% if job_list %}
<ul>
{% for job in job_list %}
<li>{{job.type_name}} <a href="/job/{{job.id}}/" style="color:blue">{{job.job_name}}</a> {{job.city_name}} </li>
{% endfor %}
</ul>
{% else %}
<p>No jobs available.</p>
{% endif %}
{% endblock %}此时页面路径并未定义, 访问会报错
添加自定义页面的视图层
向
jobs/views.py文件中添加脚本1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25# Create your views here.
from . import models
from django.http import HttpResponse
from django.template import loader
def joblist(request):
# 定义职位列表属性
# objects 是django的model类特有属性, 从数据库中提取
# order_by 表示按照某种属性排序
job_list = models.Job.objects.order_by('job_type')
# 使用django自带模板导入方法, 导入事先写好的joblist.html页面
template = loader.get_template('joblist.html')
# 定义一个上下文, 对应展示当前的职位列表
context = {'job_list': job_list}
# 遍历职位列表, 转换城市属性和职位名称
for job in job_list:
job.city_name = models.Cities[job.job_city][1]
job.type_name = models.JOB_TYPES[job.job_type][1]
return HttpResponse(template.render(context))定义应用的路径
添加
jobs/urls.py文件1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
- Main_Function: ::TODO::
- Author: ZhouRuixi
- Mail: 879582094@qq.com
- License: None
- CreateTime: 2022/4/8 13:27
- Copyright: Copyright © 2022 ChowRex. All rights reserved.
- Project: django-learning
"""
__author__ = "Chow Rex"
__copyright__ = "Copyright © 2022 Chow Rex. All rights reserved."
__credits__ = [__author__]
__license__ = "None"
__maintainer__ = __author__
__email__ = "879582094@qq.com"
__status__ = "Production"
__version__ = "0.0.1"
from . import views
from django.urls import re_path
# 以下引用已在4.0版本移除, [详情](https://docs.djangoproject.com/pl/4.0/releases/4.0/#features-removed-in-4-0)
# 来源博客: https://www.cnblogs.com/emanlee/p/15855274.html
# from django.conf.urls import url
urlpatterns = [
# 展示职位列表
re_path(r'^joblist/', views.joblist, name='joblist')
]此时页面仍旧无法访问, 因为上述定义URL只是在应用中定义了URL映射, 并未在项目中定义路由
添加项目URL路由信息
修改
recruitment/urls.py文件1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27"""recuritment URL Configuration
The `urlpatterns` list routes URLs to views. For more information please see:
https://docs.djangoproject.com/en/4.0/topics/http/urls/
Examples:
Function views
1. Add an import: from my_app import views
2. Add a URL to urlpatterns: path('', views.home, name='home')
Class-based views
1. Add an import: from other_app.views import Home
2. Add a URL to urlpatterns: path('', Home.as_view(), name='home')
Including another URLconf
1. Import the include() function: from django.urls import include, path
2. Add a URL to urlpatterns: path('blog/', include('blog.urls'))
"""
from django.contrib import admin
from django.urls import path
from django.urls import re_path, include
# 以下引用已在4.0版本移除, [详情](https://docs.djangoproject.com/pl/4.0/releases/4.0/#features-removed-in-4-0)
# 来源博客: https://www.cnblogs.com/emanlee/p/15855274.html
# from django.conf.urls import url
urlpatterns = [
re_path(r"^", include('jobs.urls')),
path('admin/', admin.site.urls),
]此时该应用已可以正常访问
增加职位详情页面
增加职位详情页面
添加jobs/templates/job.html文件
1 | {% extends 'base.html' %} |
增加URL映射, 修改jobs/urls.py文件
1 | urlpatterns = [ |
增加视图路由, 修改jobs/views.py文件
1 | def detail(request, job_id): |