django tutorial – how to change Django settings file

django tutorials

Hey guys..! welcome to another Django tutorial. you must know how to change the Django settings file when you started the Django project.

If you really don’t know how to start the Django project, I already discussed how the Django project started and the pillars of the Django project. links are below,

Django tutorials – how the Django project works

Django tutorial – Build your first App with Django python 3

In this Django tutorial, I will teach you how to change the necessary settings in the Django settings file. There are 6 things you must change when you start a Django project and when you deploy the project you must change some more settings.

here I discussed the necessary settings for development and also deployment of the Django project. they are below

  • Templates
  • Static files
  • Media files
  • Database setup
  • Timezone
  • Apps

Template setup in Django project

let’s open your settings.py in your Django project folder and open settings.py. Go to the Templates attribute and change the path like below. Make sure you must create a folder name called templates outside of the project folder.

TEMPLATES = [
    {
        
        'DIRS': [os.path.join(BASE_DIR,'templates')], # this is your template folder path.
        
    },
]

this can help you to render your templates in your Django project. all our template files like index.html, about.html, and signup.html must be inside of the templates folder.

Static files

we need to add styles, colors, and animations to our templates, so we need to configure the static files. Go to end of your settings.py file, there you find an attribute called STATIC_URL, write the below code.

STATIC_URL = '/static/'

STATICFILES_DIRS = [
    os.path.join(BASE_DIR, "static") # this is your static folder
]

STATIC_ROOT = os.path.join(BASE_DIR,'assets') #this is you assets folder.

here we are creating 2 folders called static and assets. In the static folder, we store CSS and javascript and other files regarding styling.

In assets folder we collect all the static files and images and store in it and also we render all static files and images from that one folder assets.

Media files

we need to store and retrieve media files in our project. so we definitely configure that media, so create a new folder called media. let’s configure that media in settings.py file.

MEDIA_URL ='/media/'

MEDIA_ROOT = os.path.join(BASE_DIR,'media')

Note: we must configure those static files and media files in urls.py because those are rendering through URLs. copy the below code and paste in urls.py file in the Django project folder.

from django.conf.urls.static import static
from django.conf import  settings


if settings.DEBUG:
    
    urlpatterns  += static(settings.STATIC_URL,document_root=settings.STATIC_ROOT)
    urlpatterns += static(settings.MEDIA_URL,document_root=settings.MEDIA_ROOT)

Configure the Database

In our Django project, SQLite is the default database. If we want, we can configure the other databases also. let’s change your database by using this code. Here I configure the MySQL database.

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql', 
        'NAME': 'DB_NAME', # Database name
        'USER': 'DB_USER',
        'PASSWORD': 'DB_PASSWORD',
        'HOST': 'localhost',   # Or an IP Address that your DB is hosted on
        'PORT': '3306',
    }
}

In the same way, we can configure the other databases also.

Setup your Time zone

we know we are from the same programming community python but we are from different time zones. so let’s configure your timezone in your Django project. I am from India, so I set my time zone to India. check your timezones here.

TIME_ZONE =  'Asia/Kolkata'

APP installations

when we create a new app, we must install our app in settings.py file. let’s do that configure also.

INSTALLED_APPS = [

                 'Accounts',

]

Here, Accounts is my app. whenever you create a new app, you must install a new app in the INSTALLED_APPS section.

 These are the most important configurations in settings.py when we started a new Django project. I hope you definitely like the Django tutorials.

For more Django tutorials and get free Django tutorials pdf please do subscribe to our newsletter. Thank for visiting us.

Leave a Reply