一、创建项目
djang-admin startproject day16
二、创建app
2.1创建app
python manage.py startapp app01
2.2 day16文件下sitting文件中注册app
INSTALLED_APPS = [ 'django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', 'app01.apps.App01Config' ]
2.3、查看访问地址
python manage.py runserver
三、设计数据库(day16/app01/models.py)
3.1、部门表
class Department(models.Model): # ''' 部门表 ''' title = models.CharField(verbose_name='部门标题', max_length=32)
3.2、员工表
class Userinfo(models.Model): # ''' 员工表 ''' name = models.CharField(verbose_name='姓名', max_length=16) password = models.CharField(verbose_name='密码', max_length=64) age = models.IntegerField(verbose_name='年龄') account = models.DecimalField(verbose_name='账户余额', max_digits=10, decimal_places=2, default=0) create_time = models.DateTimeField(verbose_name='入职时间') # 无约束条件 # depart_id = models.BigIntegerField(verbose_name='部门ID') # 有约束条件 与Department表id约束 # django中ForeignKey会自动为depart加_id # on_delete=models.CASCADE级联删除;models.SET_NULL置空(可空) depart = models.ForeignKey(to='Department', to_field='id', null=True, blank=True, on_delete=models.SET_NULL) # django中做约束 gender_choices = ( (1,'男'), (2,'女'), ) gender = models.SmallIntegerField(verbose_name='性别', choices=gender_choices)
四、生成数据库
4.1、生成数据文件并创建表
python manage.py makemigrations
python manage.py migrate
五、静态文件
5.1、建立static文件
app01/tatic
5.2、建立templates模板文件
app01/templates
六、部门管理
6.1、部门列表(查)
def depart_list(request): queryset = models.Department.objects.all() return render(request,'depart_list.html', {'queryset':queryset})
6.2、添加部门(增)
def depart_add(request): if request.method == 'GET': return render(request,'depart_add.html') title = request.POST.get('title') models.Department.objects.create(title=title) return redirect('/depart/list')
6.3、删除部门(删)
def depart_delete(request): nid = request.GET.get('nid') print(nid) models.Department.objects.filter(id=nid).delete() return redirect('/depart/list')
6.4、编辑部门(改)
def depart_edit(request,nid): if request.method =='GET': row_object = models.Department.objects.filter(id=nid).first() return render(request, 'depart_edit.html', {'row_object' : row_object}) title = request.POST.get('title') models.Department.objects.filter(id=nid).update(title=title) return redirect('/depart/list')