requirements.txt and the helloworld project

Learn about the requirements.txt file and the 'helloworld' project folder.

The requirements.txt file

Our requirements.txt file contains two dependencies necessary to setup Django environment on Educative:

  1. Django itself
  2. Gunicorn

This will allow pip to install the latest version of Django and the pre-specified (latest to date) Gunicorn (a Python Web Server Gateway Interface (WSGI) HTTP server) version in our docker container.

We will discuss how we use Gunicorn in more detail when we get to the start.sh file, but for now, this is all our requirements.txt file does.

On to the helloworld project folder!

helloworld

This project and directory structure needed for the simple “Hello, World!” application is generated by the django-admin command-line tool.

It has the following structure:

manage.py

Allows you to start the development test web server.

db.sqlite3

The database used in the Django project.

urls.py

Specifies what URLs route to which view.

settings.py

This file, as the name suggests, contains the Django settings for our helloworld project.

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

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

Two changes to the settings.py file

Change 1:

The first change we made in this file is to assign the ALLOWED_HOSTS variable to the following line:

ALLOWED_HOSTS = [os.environ['ALLOWED_HOSTS']]

The above line allows us to set the host which is allowed to feature our app. On Educative, every author is provided with a unique link to host their app. We will be finding more about this in the next lesson. For now, just add the above line in the settings.py file.

Change 2:

Add the following line anywhere in your settings.py file:

X_FRAME_OPTIONS = "allow-from https://educative.io"

The above line is just an educative platform requirement, without which our app won’t load in the iframe.

For now, we have already made all these changes in the settings.py file we provided in the tarball (Appendix). Whenever you make a new Django project, make the above changes in your settings.py file.

This covers what we need to know regarding our helloworld application project structure and the requirements.txt file.

The above changes are also shown in the code snippet below (highlighted lines 28 and 50).

"""
Django settings for helloworld project.
Generated by 'django-admin startproject' using Django 2.2.2.
For more information on this file, see
https://docs.djangoproject.com/en/2.2/topics/settings/
For the full list of settings and their values, see
https://docs.djangoproject.com/en/2.2/ref/settings/
"""
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 = '%!jf3(33rjwog+l5$e-xm+aonh1s@yljlfwb$$whw^a+ilc2n)'
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True
ALLOWED_HOSTS = [os.environ['ALLOWED_HOSTS']]
# Application definition
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
]
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',
]
X_FRAME_OPTIONS = "allow-from https://educative.io"
ROOT_URLCONF = 'helloworld.helloworld.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 = 'helloworld.helloworld.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'),
}
}
# 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',
},
]
# 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
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/2.2/howto/static-files/
STATIC_URL = '/static/'

In the next lesson, we’ll be talking about the Gunicorn server and the start.sh file.

Stay tuned :)