很少写博客,之前工作中曾经用过一段时间django框架,后来基本忘记了,所以今天开始打算,从头再过一遍,开始学习django框架。
首先说明开发环境python3.6+django2.2+mysql5.7,版本不同有些地方配置可能不一样,百度一下基本都可以解决问题,环境安装很简单不过多叙述。
一、用命令创建一个django项目,项目名称为SchoolManagementProject。
django-admin startproject SchoolManagementProject
二、切换到该目录下,命令行创建一个应用myApp,命令行如下:python manage.py startapp myApp。(这两步如果遇到错误可以百度解决很简单)
三、创建个名为shool的数据库(名字随意,后面保持统一就好)
四、打开pycharm,修改SchoolManagementProject目录下settings.py文件,在INSTALL_APPS的代码段下新增myApp以及修改DATABASES代码段为
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'myApp',
]
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'school',
'USER':'root',
'PASSWORD':'你的密码',
'HOST':'127.0.0.1',
'PORT':'3306'
}
}
五、
在models文件下新建数据库关联类,Grades和Students
class Grades(models.Model):
class Meta:
db_table = "grades"
ordering = ["id"]
name = models.CharField(max_length=20)
girlnum = models.IntegerField()
boynum = models.IntegerField()
isDelete = models.BooleanField(default=False)
class Students(models.Model):
class Meta:
db_table = "students"
ordering = ["id"]
name = models.CharField(max_length=20)
gender = models.BooleanField()
age = models.IntegerField()
contend = models.CharField(max_length=40)
isDelete = models.BooleanField(default= False)
#外键关联
grade = models.ForeignKey("Grades", on_delete=models.CASCADE)
六、
在SchoolManagementProject文件夹下的__init__.py文件里新增如下代码
import pymysql
pymysql.install_as_MySQLdb()
七、
执行如下命令生成迁移文件:python manage.py makemigrations
八、
执行如下命令生成表:python manage.py migrate
九、
使用如下命令创建后台管理用户:python manage.py createsuperuser
十、
修改settings文件的这两行如下:
LANGUAGE_CODE = 'zh-Hans'
TIME_ZONE = 'Asia/Shanghai'
十一、
在myApp的admin.py文件下新增如下代码
from myApp.models import Grades,Students
class StudentsInLine(admin.TabularInline):
model = Students
extra = 2
@admin.register(Grades)
class GradesAdmin(admin.ModelAdmin):
inlines = [StudentsInLine]
list_display = ["pk", "name", "girlnum", "boynum", "isDelete"]
# admin.site.register(Grades,GradesAdmin)
@admin.register(Students)
class StudentsAdmin(admin.ModelAdmin):
def gender_handler(self):
if self.gender:
return "男"
return "女"
gender_handler.short_description = "性别"
def name_handler(self):
return self.name
name_handler.short_description = "姓名"
def age_handler(self):
return self.age
age_handler.short_description = "年龄"
def contend_handler(self):
return self.contend
contend_handler.short_description = "描述"
def grade_handler(self):
return self.grade
grade_handler.short_description = "班级"
def isDelete_handler(self):
if self.isDelete:
return "是"
return "否"
isDelete_handler.short_description = "是否删除"
# 执行动作的位置
actions_on_top = False
actions_on_bottom = True
# 列表页属性
list_display = ["pk", name_handler, gender_handler, age_handler, contend_handler, grade_handler, isDelete_handler]
list_filter = ["grade"]
search_fields = ["name"]
list_per_page = 4
# 添加、修改页属性
# fields = ["name", "age", "gender", "grade", "contend", "isDelete"]
fieldsets = [
("base", {"fields":["name", "age", "gender"]}),
("more", {"fields":["grade", "contend", "isDelete"]})
]
十二,在数据库中插入数据,然后输入命令python manage.py runserver 启动程序,打开浏览器,输入 127.0.0.1/admin 可以打开后台页面,因为此时直接输入127.0.0.1 展示django的欢迎界面