2. Create Django project and APP
Command :
- Create a Django project
- django-admin startproject name
- Create sub-application
- python manager.py startup app name
2.1 Create project
When using the Flask framework, the organization and creation of the project directory need to be created manually by ourselves.
In django, the project directory can be created with the help of the commands provided by django.
2.1.1 Create
The command to create a project is:
django-admin startproject 工程名称
For example: If you want to create a project named ezfy in the source directory of the desktop, you can execute the following command:
cd ~/Desktop/source
django-admin startproject ezfy
2.2.2 Project directory description
View the created project directory, the structure is as follows:
- The directory with the same name as the project, here is ezfy.
- settings.py is the overall configuration file of the project.
- urls.py is the project’s URL configuration file.
- wsgi.py is the project's WSGI-compatible web server entry.
- manage.py is the project management file through which projects are managed.
2.2.3 Running the built-in development server
During the development phase, Django provides a lightweight web server written in pure Python, which is used only during the development phase.
Run the server command as follows:
python manage.py runserver ip:端口
或:
python manage.py runserver
2.3 Create a new application
2.3.1 Application for creating peer directory
-
Install django
pip install django==4.2
-
Create project
django-admin startproject ezfy
Where
ezfy
refers to your project name (projectname
), the directory is as shown in the figure -
Create a new demo application
django-admin startapp demo
-
settings.py
After the creation is completed, you need to register it in INSTALLED_APPS in the folder with the same name as the project (here is ezfy) .Registration name method 1:
Registration name method 2: directly the same as the application name
2.3.2 Create a new application step in the directory
Many times, creating new applications in the same level directory will result in many folders. Generally, multiple applications are developed under the apps package, that is, applications are concentrated under one package. Here is a demonstration.
-
Install django
pip install django==4.2
-
Create project
django-admin startproject ezfy
Where
ezfy
refers to your project name (projectname
), the directory is as shown in the figure -
create app
Create an app under the specified path:
Create a new apps package:
- First cd to the specified path apps
cd .\apps\
- run
django-admin startapp users
Where users refers to the name of your application. The users folder will appear next in the apps folder.
settings.py
After the creation is completed, you need to register it in INSTALLED_APPS in the folder with the same name as the project (here is ezfy) . Be sure to register!- Open the apps.py file under users under apps and modify it to the corresponding apps.users.
- Modify the name variable assignment
- Migrate applications
- run
reference
DataWhale open source artificial intelligence community
DataWhale-Sweettalk-Django4.2