django - Create Django projects and apps

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:

image-20230813162958209

  • 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

image-20230813163044387

2.3 Create a new application

2.3.1 Application for creating peer directory

  1. Install django

    pip install django==4.2
    
  2. Create project

    django-admin startproject ezfy
    

    Where ezfyrefers to your project name ( projectname), the directory is as shown in the figure

    image-20230813150233179

  3. Create a new demo application

    django-admin startapp demo
    

    image-20230813164829780

  4. settings.pyAfter 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:

    image-20230813165108094

    Registration name method 2: directly the same as the application nameimage-20230813165231305

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.

  1. Install django

    pip install django==4.2
    
  2. Create project

    django-admin startproject ezfy
    

    Where ezfyrefers to your project name ( projectname), the directory is as shown in the figure

    image-20230813150233179

  3. create app

    Create an app under the specified path:

    Create a new apps package:

    image-20230813163157988

    • 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.

    image-20230813163312058

    settings.pyAfter 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!

    image-20230813163411551

    image-20230813164027839

    • Open the apps.py file under users under apps and modify it to the corresponding apps.users.
    • Modify the name variable assignment

    image-20230813163623704

    • Migrate applications

    image-20230813164529447

    • run

    image-20230813164658741

reference

DataWhale open source artificial intelligence community
DataWhale-Sweettalk-Django4.2

Guess you like

Origin blog.csdn.net/weixin_42917352/article/details/132261827