1. 五大视图
动作 | 视图名 | 是否需要model(官方) | 是否需要form |
增 | CreateView | 是 | 是 |
删 | DeleteView | 是 | 否 |
改 | UpdateView | 是 | 是 |
查 | ListView, DetailView | 是 | 否 |
ListView是继承TemplateView
2. 开始一个小项目
项目介绍:新馆疫情登记
项目要求:列表,列出所有人员大概信息
新建app:django-admin startapp personal_info
提示:如果html记不清的话,去这个boostrap看一下,他是一个比较简单的前端库,主要是css和js的组件
代码如下
# first_project/personal_info/views.py from django.shortcuts import render # Create your views here. from django.views.generic import ListView from personal_info.models import Person class PersonList(ListView): model = Person template_name = 'personal_info/person_list.html'first_project/personal_info/views.py
# first_project/personal_info/urls.py from django.urls import path from personal_info import views app_name = 'personal_info' urlpatterns = [ path('', views.PersonList.as_view()), ]first_project/personal_info/urls.py
# first_project/peronal_info/models.py from django.db import models class Person(models.Model): # 继承Model GENDER_CHOICES = ( (1, '男'), (0, '女') ) name = models.CharField(max_length=32) # 定义VARCHAR类型字段,max_length是必须的 age = models.IntegerField() # 定义整形字段 gender = models.BooleanField(choices=GENDER_CHOICES) # 定义布尔类型变量,可选择的 id_card = models.CharField(max_length=18) address = models.CharField(max_length=256) temperature = models.FloatField() # 定义浮点型字段 class Meta: # model中的配置中心 permissions = () # 将数据库权限设置为空first_project/peronal_info/models.py
<!-- first_project/personal_info/templates/personal_info/person_list.html --> <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>疫情人员登记表</title> </head> <body> <table class="table"> <thead> <tr> <th scope="col">ID</th> <th scope="col">名字</th> <th scope="col">年龄</th> <th scope="col">性别</th> <th scope="col">疑似</th> </tr> </thead> <tbody> <!-- 默认是object_list 模板语言for循环语法 --> {% for item in object_list %} <tr> <td>{{ item.id }}</td> <td>{{ item.name }}</td> <td>{{ item.age }}</td> <!-- 不加get_display会显示数字 --> <td>{{ item.gender.get_display }}</td> <td>{% if item.temperature > 37 %}是{% else %}否{% endif %}</td> </tr> {% empty %} <tr> <!-- 意思是将5列合并成1列 --> <td colspan="5">暂无数据</td> </tr> {% endfor %} </tbody> </table> </body> </html>first_project/personal_info/templates/personal_info/person_list.html
# first_project/first_project/settings.py INSTALLED_APPS = [ 'django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', 'first_app.apps.FirstAppConfig', # 注册 fist_app 'personal_info.apps.PersonalInfoConfig', # 注册 personal_info ]first_project/first_project/settings.py
# first_project/first_project/urls.py from django.contrib import admin from django.urls import path, include from first_app import urls as first_app_urls from personal_info import urls as personal_info_urls urlpatterns = [ path('admin/', admin.site.urls), path('first_app/', include(first_app_urls, namespace='test')), path('personal_info/', include(personal_info_urls, namespace='personal_info')) ]first_project/first_project/urls.py
执行 python3 manage.py makemigrations,会遍历每个目录下models.py文件,如果有定义model,会生成相应的迁移文件
一般不需要关注生成的文件
执行 python3 manage.py migrate 迁移文件
启动服务
因为数据库中没有数据,所以是合理的