Django learning day01

Introduction to Django

Django is an open source web application framework written in Python. This system adopts the framework mode of MVC, also known as MVT mode.

1. MVC pattern

The full name of the MVC pattern is Model View Controller, which is an abbreviation for model, view, and controller. The popular understanding of the MVC pattern means that it is mandatory to separate the input, processing and output of the application.

  • Model: The data access layer is used to encapsulate the data related to the business logic of the application and the processing of the data. To put it bluntly, the model object is responsible for accessing data in the database.
  • View: The presentation layer is responsible for the display and presentation of data. Render the html page to the user, or return data to the user.
  • Controller: business logic layer. Responsible for collecting user input from the user side, performing business logic processing, including sending data to the model, and performing CRUD operations.

Illustration: Illustration
write picture description here
of the representation of MVC in the browser:
write picture description here

2. MVT pattern


  • Model: Object responsible for business and database (ORM)
  • View: Responsible for business logic and calling Model and Template appropriately
  • Template: Responsible for rendering the page to the user

Note: There is also a url dispatcher in Django, also called a route. It is mainly used to send url requests to different Views for processing, and Views are processing related business logic.

3.B/S

Browser/Server (browser/server mode)

4.C/S

Client/Server (client/server mode)

Create a virtual environment (windows)

  1. Install the virtual environment
    pip install virtualenv
  2. Create a virtual environment
    virtualenv --no-site-package filename
  3. Enter the virtual environment in the Scripts folder, enter activate , exit deactivate

Install Django

  1. Install Django
    pip install Django==1.11
  2. Create project
    django-admin startproject project name
  3. Start the Django project
    python manage.py runserver ip:port
  4. Create app
    python manage.py startapp appname
  5. app
    __init__.py: Initialize
    admin.py: Manage the background registration model
    apps.py: When registering the app in settings.py, it is recommended to use
    from app.apps import AppConfig
    AppConfig.name model.py
    : Where to write the model
    views.py: Where to write business logic
  6. migrate database (create database tables)
    create changed file python manage.py makemigrations
    apply generated py file to database python manage.py migrate
    Note: This method can create a table corresponding to the models.py code in a database such as SQL, without manually executing SQL yourself.

Files and related settings in the Django project directory

1. Create a Django project and expand its directory with the following files:

write picture description here

manage.py : It is the management set tool used by Django to manage this project. After the site runs, the database is automatically generated, and the modification of the data table is completed through this file.

__init__.py : Indicates that the directory structure is a python package with no content, and some tools will be initialized later.

settings.py: The configuration file of the Django project, which defines the reference components, project name, database, static resources, debugging mode, domain name restrictions, etc. of this project.

urls.py: The URL routing map of the project, which module responds to the client request url.

wsgi.py: Define WSGI interface information, usually this file does not need to be changed after it is generated

2.settings.py file configuration

  1. Set the language to Chinese
    LANGUAGE_CODE = 'zh-hans' for Chinese
    LANGUAGE_CODE = 'en-us' for English
  2. Set the time zone
    UTC: Universal Standard Time, which is usually referred to as the zero time zone.
    Beijing time means the time of East Eighth District, ie UTC+8
    TIME_ZONE = 'Asia/Shanghai'

Guess you like

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