This project is based on Django. Because it is interface-related development, front-end development is not involved for the time being. The back-end uses MySQL for data storage, addition, deletion, modification, and other operations.
- 1) What is Django?
- 2) Project introduction
- Student Information Management System:
- College information management module interface:
- 3) Development environment deployment
- 1. Installation of Python and Pycharm
- 2. Installation of Django and other libraries
- Installation steps in a virtual environment:
- 4) Create a project with Django
- 4.1 Use the command line to create a project
- 4.2 Create APP (application)
- 1. Register the app in settings.py
- *The role of INSTALLED_APPS
- 2. Define models
- 3. Configure Django to use mysql database
- 4. Convert the model under models.py to a data table
- 4.3 Writing views
1) What is Django?
Django is a free and open source web framework developed with python, which provides many modules commonly used in website backend development , so that developers can focus on the business part.
- Django's functions are relatively complete, defining a complete set of functions for service publishing, routing mapping, template programming, and data processing.
- Django draws on the classic MVC pattern, and also divides the interaction process into 3 levels, namely the MTV design pattern: Model, Template, and View .
- For detailed introduction, please refer to "[Practical Learning (3)] Introduction and Startup of Django Framework for Open Source Project Learning" .
2) Project introduction
Student Information Management System:
- Function : Collect personal information of students, so as to provide teachers with the status of each student's school or graduate status, and allow students to use their student ID to check their performance during school.
- Related databases : student information, course information, college information.
College information management module interface:
- Function : Manage college information, support the addition, deletion, modification, and check of college information.
3) Development environment deployment
1. Installation of Python and Pycharm
Please refer to "[Practical Learning (2)] Preparations for Open Source Project Learning" .
2. Installation of Django and other libraries
In order to avoid affecting other project version dependencies, choose to install in a virtual environment.
- The introduction of the virtual environment and the installation of Django in the virtual environment can also refer to "[Practical Learning (2)] Preparations for Learning Open Source Projects" .
The content of requirements.txt:
certifi==2017.11.5 chardet==3.0.4 coreapi==2.3.3 coreschema==0.0.4 Django==1.11 djangorestframework==3.7.3 idna==2.6 itypes==1.1.0 Jinja2==2.10 MarkupSafe==1.1.1 pytz==2017.3 requests==2.18.4 uritemplate==3.0.0 urllib3==1.22 PyMySQL==0.7.11
Installation steps in a virtual environment:
C:\Users\Facek>workon //查看当前有哪些虚拟环境 Pass a name to activate one of the following virtualenvs: ============================================================================== apitest myproject C:\Users\Facek>mkvirtualenv depapi # 新建所需的虚拟环境 created virtual environment CPython3.7.9.final.0-64 in 3843ms creator CPython3Windows(dest=C:\Users\Facek\Envs\depapi, clear=False, no_vcs_ignore=False, global=False) seeder FromAppData(download=False, pip=bundle, setuptools=bundle, wheel=bundle, via=copy, app_data_dir=C:\Users\Facek\AppData\Local\pypa\virtualenv) added seed packages: pip==21.0.1, setuptools==56.1.0, wheel==0.36.2 activators BashActivator,BatchActivator,FishActivator,PowerShellActivator,PythonActivator,XonshActivator (depapi) C:\Users\Facek>pip -V # 查看虚拟环境中pip的版本 pip 21.0.1 from C:\Users\Facek\Envs\depapi\lib\site-packages\pip (python 3.7) (depapi) C:\Users\Facek>pip list # 查看当前已有的安装包 Package Version ---------- ------- pip 21.0.1 setuptools 56.1.0 wheel 0.36.2 (depapi) C:\Users\Facek\Desktop\学习\实战\depapi>pip install -r requirements.txt --default-timeout=100 -i http://pypi.mirrors.ustc.edu.cn/simple/ --trusted-host pypi.mirrors.ustc.edu.cn # 通过requirements.txt安装库，--default-timeout避免超时问题，使用中国科学技术大学的pypi镜像
4) Create a project with Django
4.1 Use the command line to create a project
After installing Django, you should already have the available management tool django-admin . Windows can use django-admin if environment variables are not configured.
django-admin startproject 项目名称
(depapi) C:\Users\Facek\Desktop\学习\实战\depapi>django-admin startproject depapi (depapi) C:\Users\Facek\Desktop\学习\实战\depapi>tree /F # 打印该指定目录的文件树状结构图，/F 显示每个文件夹中文件的名称。 卷 Windows 的文件夹 PATH 列表 卷序列号为 709F-EA84 C:. │ requirements.txt │ └─depapi │ manage.py # 一个实用的命令行工具，以各种方式与该 Django 项目进行交互 │ └─depapi # 项目的容器 settings.py # 该 Django 项目的设置/配置 urls.py # 该 Django 项目的URL声明，一份由 Django 驱动的网站"目录" wsgi.py # 一个 WSGI 兼容的 Web 服务器的入口，以便运行你的项目 __init__.py
You can run it through python manage.py runserver 0.0.0.0:8000 to check whether the project is created normally.
You can also use Pycharm to create a project : file —> new project ---- select Django —> configuration path and project name —> configuration environment (system environment is used by default) ----> click create (complete creation)
4.2 Create APP (application)
A Django project (project) is composed of multiple applications (app). Django's APP can be understood as a module in a website . We can write each module separately as an APP, which can make the project relatively tidy and easier to maintain.
The principle of creation is basically the same as that of creating a project
python manage.py startapp app的名字.
- For the complete deletion method, please refer to: https://blog.csdn.net/huochen1994/article/details/52680067
After the creation is complete, relevant configuration needs to be carried out:
1. Register the app in settings.py
Register the app in settings.py, that is, add the app to INSTALLED_APPS in the project configuration (settings.py) .
- You can choose to add the app name directly. In most cases, adding the app name directly to INSTALLED_APPS is sufficient.
- You can also add the AppConfig subclass corresponding to the app. Only when you need to customize the configuration information for the app, you need to directly add the AppConfig subclass path corresponding to the added app to INSTALLED_APPS.
*The role of INSTALLED_APPS
INSTALLED_APPS helps django synchronize databases, run tests, make URLs work normally, and solve more related problems.
- The INSTALLED_APPS registration application is used to interact with the models/databases in models.py. If you don’t register, the view will be accessed normally. As long as it is not associated with models.py, you can access it normally without any impact.
2. Define models
Django provides good support for various databases, including: PostgreSQL, MySQL, SQLite, Oracle. Django provides a unified calling API for these databases.
3. Configure Django to use mysql database
Django default database engine is sqlite3, in addition to sqlite3, it also supports postgresql, mysql, oracle.
Modify under DATABASES in settings.py:
- Change sqlite3 in ENGINE to mysql
- mysql related configuration
And then also you need the same level directory under settings.py init incorporated in the module and configured .py:
4. Convert the model under models.py to a data table
It should be noted that Django ORM cannot operate to the database level, but can only operate to the data table, so you have to manually create the MySQL database yourself.
mysql> show databases; //查看已有数据库 ERROR 2006 (HY000): MySQL server has gone away No connection. Trying to reconnect... Connection id: 174 Current database: studentmanagementsystem +-------------------------+ | Database | +-------------------------+ | information_schema | | dailyfresh | | learn1 | | mysql | | performance_schema | | studentmanagementsystem | | sys | | test | +-------------------------+ 8 rows in set (0.04 sec) mysql> create database depapi; //创建数据库 Query OK, 1 row affected (0.00 sec) mysql> show databases; +-------------------------+ | Database | +-------------------------+ | information_schema | | dailyfresh | | depapi | | learn1 | | mysql | | performance_schema | | studentmanagementsystem | | sys | | test | +-------------------------+ 9 rows in set (0.00 sec)
To convert the model under models.py into a data table, use the command to execute:
(depapi) C:\Users\Facek\Desktop\学习\实战\depapi\depapi>python manage.py makemigrations //makemigration生成ddl（数据库模式定义语言） Migrations for 'dep': dep\migrations\0001_initial.py - Create model ClassInfo - Create model DepartmentInfo - Create model StudentInfo - Add field dep to classinfo (depapi) C:\Users\Facek\Desktop\学习\实战\depapi\depapi>python manage.py migrate //migrate执行ddl Operations to perform: Apply all migrations: admin, auth, contenttypes, dep, sessions Running migrations: Applying contenttypes.0001_initial... OK Applying auth.0001_initial... OK Applying admin.0001_initial... OK ...
python manage.py makemigrations:
- First of all, django will check the legality of creating or modifying the database . If it is illegal, it will give a prompt. For example, CharField must set max_length, and a non-empty field without a default value is added.
- Perform this operation for the first time: the migrations directory will be created under the app, and the 0001_inital.py file will be created , which records the current table creation , dependencies and other information.
- Perform the operation next time: If there are changes to model.py, the modified content will be generated under migrations as a file similar to 0002_alter_permission_name_max_length.py. The file records your modified fields and other information. If there is no change, it will prompt: No changes detected.
- Note: These changes are not migrated to the database at this time !
python manage.py migrate: Migrate the changes to the database, complete operations such as creating tables and modifying fields . After the operation is successful, a piece of data will be added to the database django_migrations table, indicating that the migration file has been migrated.
If you look at the database at this time, you will find that in addition to the tables defined in the model, the others are all tables generated by the administrator:
mysql> use depapi; Database changed mysql> show tables; +----------------------------+ | Tables_in_depapi | +----------------------------+ | auth_group | | auth_group_permissions | | auth_permission | | auth_user | | auth_user_groups | | auth_user_user_permissions | | dep_classinfo | | dep_departmentinfo | | dep_studentinfo | | django_admin_log | | django_content_type | | django_migrations | | django_session | +----------------------------+ 13 rows in set (0.00 sec)
4.3 Writing views
The main implementation logic of the background is defined here.
Because it involves interface development, it will be described in the next article "[Interface Development Actual Combat (2)] College Information Management Module Interface Development" .
[Part of the content is referenced from]
- Django tutorial: https://www.runoob.com/django/django-tutorial.html
- Should the app name be written in Django's INSTALLED_APPS, or should it be a subclass of AppConfig?: https://zhuanlan.zhihu.com/p/220440450
- Django1.8.3 does not add a new reference in INSTALLED_APPS, can still be accessed normally? : Https://segmentfault.com/q/1010000003100424
- Detailed django migration: https://blog.csdn.net/weixin_33127753/article/details/90717038