It may become very complicated if everything is put in one project, so we can create multiple applications in the same project, that is, sub-applications, and then each sub-application implements different functional modules
1. Create a sub application
First enter the project, and then execute the following command to create a sub-application
方式1:
django-admin startapp 子应用名称
例如:django-admin startapp users
方式2:
python manage.py startapp 子应用名称
例如:python manage.py startapp users
After creating the sub-application, a new folder with the same name as the sub-application will be created in the project folder, which contains some automatically generated py files
- admin.py, background management configuration
- apps.py, configure the relevant information of the current sub-application
- models.py, the user saves the database model class
- tests.py, used for development tests
- views.py, used to write Web application views
- migrations, this is a folder used to store database migration history files
2. Register the sub-application
After the sub-application is created in the previous step, it cannot be used directly, and it needs to be registered
We open the settings.py configuration file in the project folder and add the sub-application name to the INSTALLED_APPS list
INSTALLED_APPS = [
...
'users',
]