Django_ 学员管理后台系统开发

  |   0 评论   |   0 浏览

1.初始化环境

  我这边用的是 pycharm,选择 Django,新建一个虚拟环境。如下图所示
image.png

  等待一丢丢会,项目初始化成功

  image.png

  在命令行中执行 python,可以看到已经是 python3.7 了

  项目结构如下:

1.
2├── manage.py # python命令管理
3├── student # 项目配置相关
4│   ├── __init__.py
5│   ├── settings.py # 配置项
6│   ├── urls.py # 路由规则
7│   └── wsgi.py # Web Server Gateway Interface
8└── templates # 模版

2.创建一个应用 student_sys

  使用 manage 创建一个 app

  python manage.py startapp student_sys

  这时候我们的项目结构已经发生了变化

 1.
 2├── manage.py
 3├── student
 4│   ├── __init__.py
 5│   ├── __pycache__
 6│   │   ├── __init__.cpython-37.pyc
 7│   │   └── settings.cpython-37.pyc
 8│   ├── settings.py
 9│   ├── urls.py
10│   └── wsgi.py
11├── student_sys
12│   ├── __init__.py
13│   ├── admin.py
14│   ├── apps.py
15│   ├── migrations
16│   │   └── __init__.py
17│   ├── models.py
18│   ├── tests.py
19│   └── views.py
20└── templates

  下面就开始扣代码吧 ,"cheat is cheaper,show me code"

  ok,let`s go!

编写代码

model.py

  修改 model,定义相关的字段。这边是用的 python3.7,如果你用 python2 版本的需要加上相关编码声明

 1
 2from django.db import models
 3
 4# Create your models here.
 5
 6class Student(models.Model):
 7
 8SEX_ITEMS = [
 9
10(1, 'boy'),
11
12(2, 'girl'),
13
14(0, 'unknow'),
15
16]
17
18STATUS_ITEMS = [
19
20(0, 'apply'),
21
22(1, 'pass'),
23
24(2, 'refuse')
25
26]
27
28name = models.CharField(max_length=128, verbose_name='name')
29
30sex = models.IntegerField(choices=SEX_ITEMS, verbose_name='sex')
31
32profession = models.CharField(max_length=128, verbose_name='job')
33
34email = models.EmailField(verbose_name='email')
35
36qq = models.CharField(max_length=128, verbose_name='QQ')
37
38phone = models.CharField(max_length=128, verbose_name='phone')
39
40status = models.IntegerField(choices=STATUS_ITEMS, default=0, verbose_name='check_status')
41
42created_time = models.DateTimeField(auto_now_add=True, editable=False, verbose_name='created_time')
43
44def __str__(self):
45
46return '<Student: {}>'.format(self.name)
47
48class Meta:
49
50verbose_name = verbose_name_plural = 'student_info'
51

admin.py

 1
 2from django.contrib import admin
 3
 4# Register your models here.
 5
 6from .models import Student
 7
 8class StudentAdmin(admin.ModelAdmin):
 9
10list_display = ('id', 'name', 'sex', 'profession',
11
12'email', 'qq', 'phone', 'status',
13
14'created_time'
15
16)
17
18list_filter = ('name', 'status', 'created_time')
19
20search_fields = ('name', 'profession')
21
22fieldsets = (
23
24(None, {
25
26'fields': (
27
28'name',
29
30('sex', 'profession'),
31
32('email', 'qq', 'phone'),
33
34'status'
35
36)
37
38}),
39
40)
41
42admin.site.register(Student, StudentAdmin)
43

settings.py

  添加 student 到 INSTALLED_APPS

 1
 2INSTALLED_APPS = [
 3
 4'student',
 5
 6'django.contrib.admin',
 7
 8'django.contrib.auth',
 9
10'django.contrib.contenttypes',
11
12'django.contrib.sessions',
13
14'django.contrib.messages',
15
16'django.contrib.staticfiles',
17
18]
19
20.....
21

迁移创建表

  在包含 manage.py 那一级目录下执行

 1
 2python manage.py makemigrations
 3
 4python manage.py migrate
 5
 6# 迁移创建表,输出如下
 7
 8Operations to perform:
 9
10Apply all migrations: admin, auth, contenttypes, sessions
11
12Running migrations:
13
14Applying contenttypes.0001_initial... OK
15
16Applying auth.0001_initial... OK
17
18Applying admin.0001_initial... OK
19
20Applying admin.0002_logentry_remove_auto_add... OK
21
22Applying admin.0003_logentry_add_action_flag_choices... OK
23
24Applying contenttypes.0002_remove_content_type_name... OK
25
26Applying auth.0002_alter_permission_name_max_length... OK
27
28Applying auth.0003_alter_user_email_max_length... OK
29
30Applying auth.0004_alter_user_username_opts... OK
31
32Applying auth.0005_alter_user_last_login_null... OK
33
34Applying auth.0006_require_contenttypes_0002... OK
35
36Applying auth.0007_alter_validators_add_error_messages... OK
37
38Applying auth.0008_alter_user_username_max_length... OK
39
40Applying auth.0009_alter_user_last_name_max_length... OK
41
42Applying auth.0010_alter_group_name_max_length... OK
43
44Applying auth.0011_update_proxy_permissions... OK
45
46Applying sessions.0001_initial... OK
47

创建后台用户

  python manage.py createsuperuser

  到这里我们简单的管理后台就写完了

启动访问

  python manage.py runserver 8000
image.png
image.png

  今天就到这里,下面的功能后面在实现

---------------------------------------------------------------
>> 博客地址:https://blog.mufengs.com
>> 邮箱地址:[email protected]
>> 微信帐号:Do8080
>> Github : https://github.com/mufengcoding
---------------------------------------------------------------