Start the service [test development phase]

  1. Terminal CD into the project folder, for examplecd E:\DjangoProject\day01
  2. After entering the project folder, executepython manage.py runserverNote :[In this startup mode, Django starts the Django service in the foreground, listening on port 8000 by default]

  1. Browser accesshttp://127.0.0.1:8000/Note: If you want to change ports, you can use python manage.py runserver port number.

After a successful startup, you can see that djangos built-in database file db.sqlite3 is generated in this directory, which is Django’s default built-in lightweight database.

Close the service

  • In the runServer startup terminal, press Ctrl + Break. We can also see how to shut down the service on the terminal

Startup FaQs

  • Startup error

Error:That port is already in use Cause: The port is already in use. This indicates That the default listening port 8000 is already occupied by another process when Django is started

Project structure – Settings. py file introduction

  • Settings. py contains all the configuration items used to start a Django project
  • Configuration items include public configuration and custom configuration
  • Public configuration – Django official provides the basic configuration: docs.djangoproject.com/en/2.2/ref/…

We opened our project using PyCharm and went to settings.py in the project file, which we went through in turn

1. BASE_DIR

It is used to bind the absolute path of the current project day01 (project path)

BASE_DIR = Path(__file__).resolve().parent.parent
Copy the code
2. SECRET_KEY

Used for encryption is the key

SECRET_KEY = 'django-insecure-u=&#*byi*qu9cvg! q%#x5iv--7e2gm6bzqp3a6(tf(7=h#vgq%'Copy the code
3. DEBUG Configures the enabling mode for a Django project
  • DEBUG = True In The Python manage.py runserver mode, error messages are exposed during project running for easy debugging.
  • DEBUG = False Indicates that the debugging mode is disabled in an online environment. The runtime does not access the static directory, that is, it cannot load static resources.
4. ALLOWED_HOSTS indicates the permission to access domain names
  • If DEBUG = False, this field must be filled:
  • [], empty list, indicating that only 1217.0.0.1, localhost can access the project;
  • [‘*’], indicating that any network address can access the current project.
  • [‘192.168.1.3’, ‘192.168.3.3’] indicates that only two hosts can access the current project.

Note: If you are on a LAN to allow other hosts to access the site, use ALLOWED_HOSTS=[‘*’].

5. INSTALLED_APPS app list

You can add or remove them according to your project needs. Django loads in list order at startup, so keep your apps in the same order.

INSTALLED_APPS = [# built-in contrib.admin', # built-in user authentication 'django.contrib.auth', # all model metadata 'django. Contrib. Contenttypes', # session, Said that the current access to the website user identity 'django. Contrib. Sessions', # message' django. Contrib. The messages', # static resource path 'django. Contrib. Staticfiles', # periodic task 'django_celery_beat', # register your APP 'userapp',]Copy the code
6. MIDDLEWARE

I won’t go into details here. If you want to know more, you can see this website: www.jb51.net/article/136…

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',
]
Copy the code
7. ROOT_URLCONF The root URL of the current project, which is the entry to Djangos routing system.
ROOT_URLCONF = 'day01.urls'
Copy the code
TEMPLATES
TEMPLATES = [{# define template engines' BACKEND ':'. Django template. Backends. Django. DjangoTemplates', # setting template path 'DIRS' : [], # to find template file 'APP_DIRS': True, # to call function 'OPTIONS' in RequestContext: {'context_processors': [ 'django.template.context_processors.debug', 'django.template.context_processors.request', 'django.contrib.auth.context_processors.auth', 'django.contrib.messages.context_processors.messages', ], }, }, ]Copy the code
9. WSGI_APPLICATION

The full Python path to WSGI application objects that Django’s built-in server will use when the project is deployed.

WSGI_APPLICATION = 'day01.wsgi.application'
Copy the code
10. DATABASES configures the database

The default configuration is the SQLLITe3 database that comes with Django. Django supports multiple databases, and you can change the database configuration in this dictionary variable.

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': BASE_DIR / 'db.sqlite3',
    }
}
Copy the code

We can also switch to the mysql database

DATABASES = { 'default': { 'ENGINE': 'django.db.backends.mysql', 'NAME': 'root', 'USER': 'root', 'PASSWORD': '123456', 'HOST': '127.0.0.1', 'PORT': '3306',}Copy the code
11. AUTH_PASSWORD_VALIDATORS Password validators

Django uses these built-in components to avoid the problem of users setting inadequate password levels.

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',
    },
]
Copy the code
12. Time zone and language Settings
LANGUAGE_CODE = 'en-us' // English: 'en-us' or Chinese: 'zh-hans'; TIME_ZONE = 'UTC // Is the world time zone 'UTC' or China time zone 'Asia/Shanghai'. The values of the variables #USE_118N and USE_L10N indicate whether internationalization and localization needs to be enabled. It is enabled by default. USE_I18N = True //I18N stands for internationalization USE_L10N = True //L10N stands for localization. USE_TZ = True // When set to True, the time stored in the database is 'UTC'.Copy the code
13. STATIC_URL Static resource directory

Static resources include CSS, JS, and Images. For example, if you want to add images to your project, the static images will be stored in the new static directory. This allows you to access the static resource through the STATIC_URL= ‘/static/’ path.

STATIC_URL= '/static/'
Copy the code