django - configuration settings.py and related parameter description

3. Configure settings.py and related parameter descriptions

3.1 Configure the setting.py file

  1. Set the setting.py file

    add the installed library

    'apps.erp_test',
    'rest_framework',
    'django_filters',
    'drf_spectacular',
       
    

    Add new APP

     'users'
    
  2. Startup project

    # 运行项目先执行数据库相关操作,再启动 django 项目
    python manage.py makemigrations
    python manage.py migrate
    python manage.py runserver
    

3.2 Description of related parameters

3.2.1 BASE_DIR

BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))

The root directory of the current project, Django will use this to locate related files in the project. We can also use this parameter to construct the file path.

3.2.2 DEBUG

调试模式,创建工程后初始值为True,即默认工作在调试模式下。
作用:
修改代码文件,程序自动重启
Django程序出现异常时,向前端显示详细的错误追踪信息.而非调试模式下,仅返回Server Error (500)

Note: Do not run Django in debug mode when deploying online, remember to modify DEBUG=False and ALLOW_HOSTS.

3.2.3 Local language and time zone

Django supports localization processing, that is, the display language and time zone support localization.

Localization is the local custom of using the displayed language, time, etc. The localization here is Chineseization. Mainland China uses Simplified Chinese , and the time zone uses Asia/Shanghai time zone. Note that Beijing time zone is not used here.

The default language and time zone of the initialized project are English and UTC standard time zone

LANGUAGE_CODE = 'en-us'  # 语言
TIME_ZONE = 'UTC'  # 时区# 时区

Change the language and time zone to mainland China information

LANGUAGE_CODE = 'zh-Hans'
TIME_ZONE = 'Asia/Shanghai'

3.3 Static files

The CSS, images, and js in the project are all static files. Generally, static files are placed in a separate directory to facilitate management. When calling in an html page, you also need to specify the path of the static file. Django provides a parsing method to configure the static file path. Static files can be placed in the project root directory or in the application directory. Since some static files are common in projects, it is recommended to place them in the project root directory for easy management.

In order to serve static files, two parameters need to be configured:

  • STATICFILES_DIRS stores the directory where static files are searched
  • STATIC_URL URL prefix for accessing static files

Example

1) Create a static directory in the project root directory to save static files.

2) Modify the two parameters of the static file in ezfy/settings.py as follows:

STATIC_URL = '/static/'
STATICFILES_DIRS = [
    os.path.join(BASE_DIR, 'static'),
]

3) At this time, any static files added in static can be accessed using the URL **/static/path of the file in static**.

For example, if we add an index.html file to the static directory, we can use 127.0.0.1:8000/static/index.html to access it in the browser.

Or we add a subdirectory and file book/detail.html in the static directory, which can be accessed in the browser using 127.0.0.1:8000/static/book/detail.html.

3.4 App application configuration

The apps.py file is included in each application directory, which is used to save the relevant information of the application.

When creating an application, Django will write a configuration class of the application to the apps.py file, such as

from django.apps import AppConfig


class BookConfig(AppConfig):
    name = 'user'

We add this class to the INSTALLED_APPS list in the project settings.py, indicating that the application with this configuration property is registered and installed.

  • The AppConfig.name attribute indicates which application the configuration class is loaded into. Each configuration class must contain this attribute, which is automatically generated by default.
  • The AppConfig.verbose_name attribute is used to set the intuitive and readable name of the application, which will be displayed in the Admin management site provided by Django, such as
from django.apps import AppConfig

class UsersConfig(AppConfig):
    name = 'user'
    verbose_name = '图书管理员'

reference

DataWhale open source artificial intelligence community
DataWhale-Sweettalk-Django4.2

Guess you like

Origin blog.csdn.net/weixin_42917352/article/details/132263270