django basic case and explanation

django basic case and explanation

django basic content

1. Django's command to create a project:
Django admin startproject project project name
python startapp app name
After 2Django creates the project, what are the components under the project folder (understanding of mvt)? is the entry of project operation, specifying the path of configuration file
__ is an empty file whose purpose is that this directory can be used as a package.
settings. Overall configuration file of Py project is the routing profile of the project is the WSGI compatible Web server of the project.
3. Understanding of MVC and MTV
first; For MVC
M: model model to interact with the database
5: View view, which is responsible for generating html files and transferring them to the page
C: The controller receives the request, processes it, interacts with M and V, and returns the response

The user clicks the registration button to send the content to be registered to the server of the website.
2. The View view receives the registration data sent by the user, and tells the Model to save the user's registration information into the database.
3. The Model layer saves the user's registration information to the database.
4. The database returns the saved results to the Model
5. Model sends the saved results to the View view.
6. The View view tells the Template to generate an Html page.
7. Template generates html content and returns it to the View view
8. View returns the html page content to the browser.
9. The browser parses and displays the html page content returned by view.

MTV evolved according to MVC

models in Django uses ORM to query the table of Mysql
all(): returns all data in the table corresponding to the model class.
get(): returns a piece of data that meets the conditions in the table. If multiple pieces of data are found, throw an exception: MultipleObjectsReturned,
If no data can be queried, throw an exception: DoesNotExist.
filter(): write query conditions with parameters and return QuerySet set data that meets the conditions

Fuzzy query like
Example: query the books whose title contains' biography '. contains

contains BookInfo. objects. Filter (btitle_contains ='pass')
Null query where field name isnull

Range query where id in (1, 3, 5)

Compare query gt lt(less than) gte(equal) lte

5. exclude: return data that does not meet the conditions.


A series of basic tutorials provided on Django's official website: it is recommended that interested students can go through these basic tutorials. These basic tutorials are indispensable in the subsequent Web development process.

django-docker                ## Project root path
│  .gitignore                    # git ignores files that are not uploaded
│  docker-compose.yml            # Docker compose file
│  Dockerfile                    # Deploy dockerfile of django project
│                     # Project Readme description
│  requirements.txt              # Files that the project must install
├─nginx                      ## nginx container configuration file
│  │  nginx.conf                 # /etc/nginx/nginx.conf configuration file
│  │
│  └─conf                        # /etc/nginx/conf.d configure nginx folder
│          default.conf
└─web                        ## web container for deploying django project
    │  uwsgi.ini                 # uwsgi configuration file for django project 
    │  │
    │  │
    │  │
    │  │               # Configure cellery task file
    │  │
    │  │
    │  │
    │  │
    │  │
    │  ├─migrations
    │  │        # Introducing celery
    │  │
    │  └─templates
    │      └─demoapp
    │              celery_detail.html          # View the specific celery execution result page
    │              celery_index.html           # View the corresponding cell task page
    │              index.html                  # Project main page
             # celery profile

1.2 description of project documents

1. Initialize a django project

1) Project documents
2) celery configuration file

2. Configuration files related to nginx container

3. web configuration file

  4,docker-compose. File YML


1.3 docker-compose.yml explanation 1. Docker compose YML detailed explanation

version: '3'                       # cocker compose version number

services:                          # Top level profile
  mysql:                           # Service Name: container construction, communication and management
    image: mysql:5.7               # Introducing official mysql image
      - ./mysql:/var/lib/mysql         # Put the in the current folder/ The mysql folder is attached to the / var/lib/mysql path of the docker container
      - "3306"                        # Expose port 3306 of the current container to the container link ed to this container
    restart: always                   # When the host restarts, the docker container is automatically pulled up
      - MYSQL_ROOT_PASSWORD=root             # mysql server root password root
      - MYSQL_DATABASE=djangodocker          # Create database djangolocker
      - MYSQL_USER=django                    # Create a user django
      - MYSQL_PASSWORD=django                # The user password is django

    image: nginx:alpine
      - ./nginx/nginx.conf:/etc/nginx/nginx.conf
      - ./nginx/conf:/etc/nginx/conf.d
      - ./web/staticfiles:/django_static
      - "80:80"                             # Bind port 80 of the container to port 80 of the host
      - web                                 # You must start the web container before you can start the nginx container

    image: redis:alpine
      - "6379"
    restart: always

    build: .
    # command: python runserver 0:8000
    # ports:
    #   - "8000:8000"
    command: uwsgi --ini uwsgi.ini             # Start uwsgi command
    working_dir: /code/web                     # Project work path
      - .:/code                                # Mount all files in the current folder to the / code folder of the container
      - "8000"
    depends_on:                                # The mysql and reids container must be started before the web container can be started
      - mysql
      - redis

    build: .
    command: celery -A web worker -l info
    working_dir: /code/web
      - .:/code
      - mysql
      - redis

‘’‘1. Comparison between expose and ports

ports: bind the port of the container to the port of the host, so that the service of the docker container can be accessed on the Internet

Expose: expose port 3 of the current container to the container link ed to the container. Expose will not expose the port to the host

‘’‘2. depends_ Difference between on and links

depends_on: Specifies that the container on which this container depends must be started first

links: ensure that the container can be accessed if the ip address changes (it has been basically discarded because it can still be accessed through the container name without using link)

Copy code

Tags: Django

Posted by zytex on Sun, 15 May 2022 17:31:10 +0300