返回文章列表文章索引阅读全文
STARBLOG / 阅读手记
0_Python-Web-Django入门系列笔记

Python-Web-Django: settings.py Configuration

Install REST Framework pip install djangorestframework pip install django-rest-swagger After installing REST Framework, the previously created Django App(hello_app) can communicate using the REST API. All Apps we create must be added to the INSTALLED_APPS field. INSTALLED_APPS = [ #

曦远 (DealiAxy)约 8 分钟
目录8 个章节

Install REST Framework

pip install djangorestframework
pip install django-rest-swagger

After installing REST Framework, the previously created Django App (hello_app) can communicate using the REST API. All Apps we create must be added to the INSTALLED_APPS field.

INSTALLED_APPS = [
    # REST Framework
    'rest_framework',
    'rest_framework_swagger',
    # Custom App
    'hello_app'
]

Database Configuration

Django supports multiple backend databases (MySQL, Oracle, PostgreSQL, etc.). I'll still use the default SQLite first, as it's more convenient.

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

Web Page Templates

To render web pages in Django, you first need to provide templates. Set the HTML template folder in DIRS; here we conventionally use the templates folder.

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [os.path.join(BASE_DIR, 'templates')],
        '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',
            ],
        },
    },
]

Static Resources

MEDIA_ROOT = os.path.join(BASE_DIR, 'static')
MEDIA_URL = ''
STATIC_ROOT = ''
STATIC_URL = '/static/'
STATICFILES_DIRS = (os.path.join(BASE_DIR, 'static'),)

Site URL Settings

Used for the server's URL routing configuration. In the code below, hello_django.urls refers to the urls.py file of the hello_django project.

ROOT_URLCONF = 'hello_django.urls'

Logging Configuration

LOGGING = {
    'version': 1,
    'disable_existing_loggers': True,
    'formatters': {
        'standard': {
            'format': '%(asctime)s %(levelname)s %(name)s %(message)s',
        },
    },
    'handlers': {
        'default': {
            'level': 'DEBUG',
            'class': 'logging.handlers.RotatingFileHandler',
            'filename': 'hello_django.log',
            'maxBytes': 1024*1024*10,  # 10 MB
            'backupCount': 5,
            'formatter': 'standard'
        },
    },
    'loggers': {
        '': {
            'handlers': ['default'],
            'level': 'DEBUG',
            'propagate': True
        }
    }
}

After configuring log output, all output using the logging library will be recorded in the hello_django.log file.


At this point, the configuration items in settings.py are basically all set up.

About

For more interesting operations, please follow my WeChat official account: DealiAxy Every article is also collected on my blog: blog.deali.cn

_EOF_

Python-Web-Django: settings.py Configuration

本文作者:
程序设计实验室
版权声明:
本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!

相关文章

讨论

共 0 条

良言一句三冬暖,恶语伤人六月寒。

加载评论中…