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 manage.py startapp app name
After 2Django creates the project, what are the components under the project folder (understanding of mvt)?
manage.py is the entry of project operation, specifying the path of configuration file
__ init.py is an empty file whose purpose is that this directory can be used as a package.
settings. Overall configuration file of Py project
urls.py is the routing profile of the project
wsgi.py 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

BookInfo.objects.filter(btitle__isnull=False)
Range query where id in (1, 3, 5)

BookInfo.objects.filter(id__in=[1,3,5])
Compare query gt lt(less than) gte(equal) lte

BookInfo.objects.filter(id__gte=3)
5. exclude: return data that does not meet the conditions.

BookInfo.objects.exclude(id=3)

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.

https://docs.djangoproject.com/en/2.1/intro/tutorial01/

https://docs.djangoproject.com/en/2.1/intro/tutorial02/

https://docs.djangoproject.com/en/2.1/intro/tutorial03/

https://docs.djangoproject.com/en/2.1/intro/tutorial04/

https://docs.djangoproject.com/en/2.1/intro/tutorial05/

https://docs.djangoproject.com/en/2.1/intro/tutorial06/

https://docs.djangoproject.com/en/2.1/intro/tutorial07/

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
│  README.md                     # 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
    │  manage.py
    │  uwsgi.ini                 # uwsgi configuration file for django project 
    │
    ├─demoapp
    │  │  admin.py
    │  │  apps.py
    │  │  models.py
    │  │  tasks.py               # Configure cellery task file
    │  │  tests.py
    │  │  urls.py
    │  │  views.py
    │  │  __init__.py
    │  │
    │  ├─migrations
    │  │      __init__.py        # 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
    │
    └─web
            celery.py           # celery profile
            settings.py
            urls.py
            wsgi.py
            __init__.py

1.2 description of project documents

1. Initialize a django project

1) Project documents

urls.py
demoapp/urls.py
demoapp/views.py
2) celery configuration file

web/init.py
web/celery.py
demoapp/tasks.py
2. Configuration files related to nginx container

django-docker\nginx\nginx.conf
django-docker\nginx\conf\default.conf
3. web configuration file

django-docker\Dockerfile
django-docker\web\uwsgi.ini
  4,docker-compose. File YML

docker-compose.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
    volumes:
      - ./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
    expose:
      - "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
    environment:
      - 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

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

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

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

  celery:
    build: .
    command: celery -A web worker -l info
    working_dir: /code/web
    volumes:
      - .:/code
    depends_on:
      - 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