Django simple usage

  1. Create a Django project django-admin startproject xxx Create a project named xxx

    View the default directory structure manage.py: It is a command line tool used by Django to manage this project. After that, the site operation and automatic database generation are completed through this file.

    xxx / init.py tells python that this directory is a python package, and there is no content. It may be used for initialization of some tools later.

    xxx /settings.py The configuration file of the Django project, which defines the components, project name, database, static resources, etc. referenced by this project in the default state.

    xxx /urls.py maintains the project's URL routing map, which defines which module responds when a client accesses it.

    xxx /wsgi.py defines the interface information of WSGI, which is mainly used for server integration. Usually, this file does not need to be changed after it is generated.

  2. Test server start python manage.py runserver [ip:port]

    You can directly run the service. The default execution port is 8000. You can also specify the ip and the port ip as 0.0.0.0. When the ip is specified as 0.0.0.0, it matches all the ip of the machine.

    Browser access: localhost:8000 You can see that the server started successfully

  3. The concept of data migration migration: it is the process of mapping the model to the database

    Generate migrations: python manage.py makemigrations

    Execute the migration: python manange.py migrate

  4. Create an application python manage.py startapp XXX Create an application named XXX Before using the application, you need to configure the application into the project, and add the application to the INSTALLED_APPS option in settings.py

  5. Application directory introduction init.py : there is no content in it, making app a package

    admin.py: The declaration file of the management site model, the default is empty

    apps.py: The application information definition file, in which AppConfig is generated, this class is used to define data such as the application name

    models.py: add model layer data class file

    views.py: Define URL corresponding functions (routing rules)

    migrations package: automatically generated, which generates migration files

    tests.py: test code file

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324662451&siteId=291194637