Django Tutorial: Django Settings.py File Explained

Publicado em: 15 Julho 2016
no canal de: Code master
16,458
112

In this Django tutorial, we will explain the Django settings.py file. In this tutorial, we will go through each part of the settings.py file which is used to control most of Django project. There is a lot to be covered in this file and we will try to cover as much information as possible. It's important to understand we can not cover everything in a single tutorial. Let's dive into the settings.py file.

The Basic Settings.py File
Below you will find the default settings.py file that ships with Django 1.9. We will go from top to bottom and try to cover everything about the settings.py file.

"""
Django settings for lpt project.

Generated by 'django-admin startproject' using Django 1.9.7.

For more information on this file, see
https://docs.djangoproject.com/en/1.9...

For the full list of settings and their values, see
https://docs.djangoproject.com/en/1.9...
"""

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/1.9...

SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = '8ub$l1nip#d(m43q%swlj3_-d(stl$7zx75cs3+%c$&la5zen)'

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_CLASSES = [
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
]

ROOT_URLCONF = 'lpt.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 = 'lpt.wsgi.application'


Database
https://docs.djangoproject.com/en/1.9...

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


Password validation
https://docs.djangoproject.com/en/1.9...

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/1.9...

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/1.9...

STATIC_URL = '/static/'
Settings.py File Explained
Python os module

import os
This line imports Pythons os module which allows for a portable way to interact with systems operating system. The main purpose of this module is to allow for cross-platform functionality when working with filenames, paths, and directories. You will see os.path a couple times in the settings file which handles the cross-platform paths for the requested files or directories. This is helpful for when we go from development to the production server.

Base_Dir

Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
Here we create a variable and this variable will represent the location of our base directory. You may be wondering what is our base directory well its lpt. Now I really have you confused, If you look we actually have three directories called lpt. Let's figure out which one of the lpt directories BASE_DIR variable represents and rename that file. Add the following line of code right below the BASE_DIR variable print("Location of base_dir", BASE_DIR) and then run the server using the command python manage.py runserver. Look at the example below.


Nesta página do site você pode assistir ao vídeo on-line Django Tutorial: Django Settings.py File Explained duração hora minuto segundo em boa qualidade , que foi baixado pelo usuário Code master 15 Julho 2016, compartilhe o link com seus amigos e conhecidos, no youtube este vídeo já foi visto 16,458 vezes e gostou 112 espectadores. Boa visualização!