Introduction to settings.py in dajngo
The settings.py file in Django is a Python module that contains various settings for your Django project. It is located in the root directory of your project, alongside the urls.py and wsgi.py files.
Here are some of the most common settings you'll find in a settings.py file:
-
DEBUG: A boolean value that determines whether or not debugging mode is enabled. When debugging is enabled, Django will display detailed error pages when an exception occurs. -
SECRET_KEY: A secret string used to provide cryptographic signing for secure data in your application. -
ALLOWED_HOSTS: A list of hostnames that the Django application is allowed to run on. -
INSTALLED_APPS: A list of all the applications installed in your Django project. -
DATABASES: A dictionary containing the database settings for your project. -
STATIC_URLandSTATICFILES_DIRS: Settings related to serving static files such as CSS, JavaScript and images. -
MEDIA_URLandMEDIA_ROOT: Settings related to serving user-uploaded media files such as images and videos. -
TIME_ZONE: The time zone for your project. -
LANGUAGE_CODE: The default language code for your project. -
TEMPLATES: A list of templates engines installed in your project and their configurations. -
MIDDLEWARE: A list of middleware classes used by your project. -
AUTHENTICATION_BACKENDS: A list of authentication backends used by your project.
There are many other settings available in Django's settings.py file, and you can also define your own custom settings as needed. It's important to be careful when modifying these settings, as they can have a significant impact on the behavior of your application.
some of the concept of settings.py file
from decouple import config
SECRET_KEY = config("SECRET_KEY")
TEMPLATE_DIR=os.path.join(BASE_DIR,'templates')
STATIC_DIR=os.path.join(BASE_DIR,'static')
INSTALLED_APPS = [
'rest_framework.authtoken',
"django_filters",
'rest_framework_simplejwt',
'blog.apps.BlogConfig',
'django.contrib.humanize',
'corsheaders', #pip install django-cors-headers
"rest_framework",
'fcm_django',
"django_crontab",
"appname.apps.AppnameeConfig",
]
AUTH_USER_MODEL = 'accounts.MyUser'
SITE_ID = int(config("SITE_ID"))
MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'corsheaders.middleware.CorsMiddleware',
]
CORS_ALLOW_ALL_ORIGINS = True
#for firebase setting
from firebase_admin import initialize_app
FIREBASE_APP = initialize_app()
os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = os.path.join(
BASE_DIR, "fcm-admin.json")
FCM_DJANGO_SETTINGS = {
"APP_VERBOSE_NAME": "Installed Devices",
"ONE_DEVICE_PER_USER": False,
"FCM_SERVER_KEY": 'AAAAb9C_abw:APA91bHjlNiIE5xFZqTG0wy91kb8rF408Vb3WhPSDQjl9lV4uuN3Zt30HyXEHUu9zFs3zvcT2G9x0E0_NK4MJw5VWqkJzl5U_HAHJTrfUeizdG2XqDiwqymUxdJp3phefXBy9NvsfJTL',
"DELETE_INACTIVE_DEVICES": True,
}
#restframework setting
REST_FRAMEWORK = {
'DEFAULT_PAGINATION_CLASS': 'rest_framework.pagination.LimitOffsetPagination',
'PAGE_SIZE': 100,
'DEFAULT_AUTHENTICATION_CLASSES': [
'accounts.middleware.SafeJWTAuthentication',
#'rest_framework.authentication.TokenAuthentication',
# 'rest_framework.authentication.BasicAuthentication',
],
'DEFAULT_PERMISSION_CLASSES': [
'rest_framework.permissions.IsAuthenticated',
],
'COERCE_DECIMAL_TO_STRING': False,
'DEFAULT_FILTER_BACKENDS': ['django_filters.rest_framework.DjangoFilterBackend']
}
#database settings
#pip install mysqlclient
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': config('NAME'),
'USER': config('DB_USER'),
'PASSWORD': config('PASSWORD'),
'HOST': config('HOST'),
'PORT': config('PORT'),
}
}
#for sqlite db
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': BASE_DIR / 'db.sqlite3',
}
}
#for postegresql
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'college_project',
'USER': 'postgres',
'PASSWORD': 'logic',
'HOST': 'localhost',
'PORT': '5432',
}
}
#JWT settings
SIMPLE_JWT = {
'USER_ID_FIELD': 'uuid',
'ACCESS_TOKEN_LIFETIME': timedelta(days=3),
'REFRESH_TOKEN_LIFETIME': timedelta(days=7),
'ROTATE_REFRESH_TOKENS': True,
'AUTH_HEADER_TYPES': ('JWT',),
}
REST_USE_JWT = True
ACCOUNT_USER_MODEL_USERNAME_FIELD = None
ACCOUNT_EMAIL_REQUIRED = True
ACCOUNT_USERNAME_REQUIRED = False
ACCOUNT_AUTHENTICATION_METHOD = 'email'
#settings for cronjob
CRONJOBS = [
('15 5 * * *', 'book.middleware.exchangerate'),
('15 5 * * *', 'book.middleware.scheduled_payment_email'),
]
USE_I18N = True
USE_L10N = True
USE_TZ = False
TIME_ZONE = 'Asia/Kathmandu'
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static/staticfiles')
STATICFILES_DIRS=[STATIC_DIR]
MEDIA_URL = "/media/"
MEDIA_ROOT = os.path.join(BASE_DIR, "media"
#email setting
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_HOST = config('EMAIL_HOST')
EMAIL_PORT = 587
EMAIL_HOST_USER = config('EMAIL_HOST_USER')
EMAIL_HOST_PASSWORD = config('EMAIL_HOST_PASSWORD')
EMAIL_USE_TLS = True
EMAIL_USE_SSL = False
DEFAULT_FROM_EMAIL = config('DEFAULT_FROM_EMAIL')
BASE_URL = config('MY_URL')
LOGIN_URL='login'
LOGOUT_REDIRECT_URL = 'home'