Django study notes

Another week later, it's time to sum up....

After creating a new project and application, I can't understand the function of each file. I created a new project and application folder structure as follows:

C:\Users\Administrator\Documents\jiaoben\DJIANGO_TEST\second>django-admin startapp second_app

C:\Users\Administrator\Documents\jiaoben\DJIANGO_TEST\second>tree . /F
 volume OS Folder for PATH list
 The volume serial number is 00000047 4417:66B1
C:\USERS\ADMINISTRATOR\DOCUMENTS\JIAOBEN\DJIANGO_TEST\SECOND
│  manage.py
│
├─second
│      settings.py
│      urls.py
│      wsgi.py
│      __init__.py
│
└─second_app
    │  admin.py
    │  apps.py
    │  models.py
    │  tests.py
    │  views.py
    │  __init__.py
    │
    └─migrations
            __init__.py

1.manage. The directory where the PY file is located is called the root directory, and all kinds of app s created are in the root directory, manage The PY file is used as a channel for various file operations of the project, such as the command Python manage Py XXXX confirms its role and implements some commands through it.

2. And manage The file in the PY peer directory is a folder with the same name as the project name. It manages the main files of the project, such as the second folder above, which contains four files

settings. The content in py is like this

import os

# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))


# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/2.2/howto/deployment/checklist/

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = '1(nk=3j+!$l!)&d6ub(4)h^_079tv+toz%#jrgcp)t16tlg5ml'

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True

ALLOWED_HOSTS = []


# Application definition

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
]

MIDDLEWARE = [
    'django.middleware.security.SecurityMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
]

ROOT_URLCONF = 'second.urls'

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [],
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
            ],
        },
    },
]

WSGI_APPLICATION = 'second.wsgi.application'


# Database
# https://docs.djangoproject.com/en/2.2/ref/settings/#databases

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
    }
}


# Password validation
# https://docs.djangoproject.com/en/2.2/ref/settings/#auth-password-validators

AUTH_PASSWORD_VALIDATORS = [
    {
        'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
    },
]


# Internationalization
# https://docs.djangoproject.com/en/2.2/topics/i18n/

LANGUAGE_CODE = 'en-us'

TIME_ZONE = 'UTC'

USE_I18N = True

USE_L10N = True

USE_TZ = True


# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/2.2/howto/static-files/

STATIC_URL = '/static/'

Statement: what I didn't write is something I don't understand. I hope the great God passing by can give me a word. Starting from "ALLOWED_HOSTS = []", this sentence means that your website can be accessed by hosts in the list. A null value indicates the local host, "ALLOWED_HOSTS = [" * "]" indicates that it is open to the outside world. Of course, you can also change it. The format added is the IP address string; The list of "INSTALLED_APPS" indicates the name of the app application loaded by the project, which is the app created under the root directory. If you just create it but don't write it in the list, this app won't work in the project; "ROOT_URLCONF = 'second.urls'" literally means root route. I understand that when accessing your website, the server enters your website from this route, second URLs denotes URLs in the second file Py file; The database list is used to set your database. The default database of django is sqlite3. Of course, you can also change it to MySQL, as follows:

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'database_name',#Database name and creation method are described in the next section
        'USER': 'root',#user name
        'PASSWORD': '123456',#Database password
        'HOST': '127.0.0.1',#Host ip address
        'PORT': '5555',#The default port is 3306. I changed it to 5555 in the configuration file
    }
}

Database is the access address of your website data in the future, which is very important; "Language_code = 'en US'" refers to the English management interface. Change the cigarette to Chinese and change it to "zh Hans", and "time_zone =' UTC '" refers to the time zone. If you want to use Beijing time, change the time zone to "time_zone =' Asia / Shanghai '" and "USE_TZ=False"

urls. The content of Py is as follows:

from django.contrib import admin
from django.urls import path

urlpatterns = [
    path('admin/', admin.site.urls),
]

urls. The website route is stored in the PY file. If it is not modified, the admin administrator interface is opened. After the app application is created in the root directory, you can add the app path, such as creating second externally_ App application, the added wording is:

from django.contrib import admin
from django.urls import path
import second_app

urlpatterns = [
    path('admin/', admin.site.urls),
    path(r'', second_app.urls),

]

My own understanding is that python execution statements are scanned from top to bottom, and the routes in django match regularly. Therefore, it is best not to duplicate the name of the app, otherwise there will be errors when accessing the website (I hope the boss will correct)__ int__.py file is an initialization file, which is as follows:

 

 

 

You're right. It's empty. I just heard that it can tell you, "this file is a python package and can be referenced". I don't know what the meaning of these words is and what the specific role of the file is. I hope the boss can give you some advice. wsgi. The PY file doesn't know what it does. I just heard that it's best not to touch it. Summarize the functions of the files in the app next time, and wait until I get it clear.

Keywords: Python Django

Added by speedyslow on Fri, 14 Jan 2022 16:18:43 +0200