Django Tutorial 2 – MyNotes App

Share the blog with your friends
  •  
  •  
  •  
  •  
  •  
  •  
  •  

In this lesson, We are going to see how to create the Django project structure and files to update for our project. Since I am not going to brief about how to install Django, I assume you have installed Django in a virtual environment. Here I have create the environment with name (djangoenv) and Python version 3.6.

Create the Django project using the below command.

django-admin startproject mynotes

Above command should have created a directory with the name mynotes with the below folder structure.

mynotes
├── manage.py
└── mynotes
    ├── __init__.py
    ├── settings.py
    ├── urls.py
    └── wsgi.py

1 directory, 5 files

There will be same project named folder inside mynotes project which has the project level settings including URLs etc. We will look into detail what are the individual files. Using manage.py script we will be managing the Django project. Next we will be cretaing notesapp. For that we need to run the below command. After creating the app, below is the folder structure.

.
├── mynotes
│   ├── manage.py
│   └── mynotes
│       ├── __init__.py
│       ├── settings.py
│       ├── urls.py
│       └── wsgi.py
└── notesapp
    ├── admin.py
    ├── apps.py
    ├── __init__.py
    ├── migrations
    │   └── __init__.py
    ├── models.py
    ├── tests.py
    └── views.py

4 directories, 12 files

We have gone through how to create the Django project and the Django application. In this lesson, we are going to see what are the different sections on settings.py file and the meaning of it.

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/2.2/howto/deployment/checklist/

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = '7k98-5nzy_u5qh07^qd^ddg4o4bf^0)0gw17=f9swv)2v73q%&'

BASE_DIR is the variable which holds the project base path. We will be referring this variable to add other variable like static and template directories in future.
SECRET_KEY is the value which Django will use to secure the confidential information like password inside the project. This should be kept secret and should not be shared with anyone else when the project released to production.

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True

ALLOWED_HOSTS = []

DEBUG parameter will be True by default because it helps during development to troubleshoot the issues which comes in a way. But when we deploy our project in production, this should be disabled.

ALLOWED_HOSTS are the list of IP addresses which the deployment server has. Per say, if the deployment server has more than network, all the IPs should be added here.

# Application definition

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'notesapp'
]

INSTALLED_APPS is the list of enabled Django apps. If we add the app name here, then only it will be considered for the projects. Most of the blogs online would say Django has “Batteries included” concept which is nothing but “notesapp” is the battery. When you want to attach or detach the app, it can be done simply changing this setting.

MIDDLEWARE = [
    'django.middleware.security.SecurityMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
]

MIDDLEWARE is the module which comes before sending the browser request to Django to process and after browser triggered the request. Hence that’s called middleware. If there is a need to process some information before sending the request to Django, it will be done through middleware. If you see the above list of Django middlewares, it is clearly noticeable that all these are security mechanisms to be implemented before the browser request reaches the Django core.

ROOT_URLCONF = 'mynotes.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',
            ],
        },
    },
]

ROOT_URLCONF specifies what is the main urls.py file to be considered for the project. We will see this in detail in the upcoming lessons.
TEMPLATES specifies how the frontend template to be rendered in Django project.

WSGI_APPLICATION = 'mynotes.wsgi.application'


# Database
# https://docs.djangoproject.com/en/2.2/ref/settings/#databases

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

WSGI_APPLICATION specifies the webservice gateway interface name to be used in production.
DATABASES specifies the list of databases to be used by our project. By default Django project configured to use SQLite as backend database.

# Password validation
# https://docs.djangoproject.com/en/2.2/ref/settings/#auth-password-validators

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',
    },
]

AUTH_PASSWORD_VALIDATORS specifies the list of password validation modules to be used in the project. Basically this is a password policy for the project.

# Internationalization
# https://docs.djangoproject.com/en/2.2/topics/i18n/

LANGUAGE_CODE = 'en-us'

TIME_ZONE = 'UTC'

USE_I18N = True

USE_L10N = True

USE_TZ = True

The above settings are used to specify the timezone and the language information.

STATIC_URL = '/static/'

STATIC_URL configuration will be used while specifying the static assets like CSS, JS and others. We will be looking into this usage while we develop the front-end for our project.

  •  
  •  
  •  
  •  
  •  
  •  
  •  

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *