How to make your first Python and Django Web App

Subba Lakshmi
5 min readFeb 9, 2020

What is Django?

Django (/ˈdʒæŋɡoʊ/ jang-goh) is a free and open source web application framework, written in Python. A web framework is a set of components that helps you to develop websites faster and easier.

When you’re building a website, you always need a similar set of components: a way to handle user authentication (signing up, signing in, signing out), a management panel for your website, forms, a way to upload files, etc.

Luckily for you, other people long ago noticed that web developers face similar problems when building a new site, so they teamed up and created frameworks (Django being one of them) that give you ready-made components to use.

Frameworks exist to save you from having to reinvent the wheel and to help alleviate some of the overhead when you’re building a new site.

Django installation

Virtual environment

Before we install Django we will get you to install an extremely useful tool to help keep your coding environment tidy on your computer. It’s possible to skip this step, but it’s highly recommended. Starting with the best possible setup will save you a lot of trouble in the future!

So, let’s create a virtual environment (also called a virtualenv). Virtualenv will isolate your Python/Django setup on a per-project basis. This means that any changes you make to one website won’t affect any others you’re also developing. Neat, right?

All you need to do is find a directory in which you want to create the virtualenv; your home directory, for example. On Windows, it might look like C:\Users\Name\ (where Name is the name of your login).

NOTE: On Windows, make sure that this directory does not contain accented or special characters; if your username contains accented characters, use a different directory, for example, C:\myfirstdjangoapp.

For this tutorial, we will be using a new directory myfirstdjangoapp from your home directory:

command-line

$ mkdir myfirstdjangoapp
$ cd myfirstdjangoapp

We will make a virtualenv called myvenv. The general command will be in the format:

command-line

$ python3 -m venv myvenv

Virtual environment: Windows

To create a new virtualenv, you need to open the command prompt and run python -m venv myvenv. It will look like this on the command-line:

C:\Users\Name\myfirstdjangoapp> python -m venv myvenv

Working with virtualenv

The command above will create a directory called myvenv (or whatever name you chose) that contains our virtual environment (basically a bunch of directory and files).

Working with virtualenv: Windows

Start your virtual environment by running the following on command-line:

C:\Users\Name\myfirstdjangoapp> myvenv\Scripts\activate

Working with virtualenv: Linux and OS X

Start your virtual environment by running the following on command-line:

$ source myvenv/bin/activate

NOTE: Remember to replace myvenv with your chosen virtualenv name!

Installing Django

Now that you have your virtualenv started, you can install Django.

Before we do that, we should make sure we have the latest version of pip, the software that we use to install Django:

command-line

(myvenv) ~$ python -m pip install --upgrade pip

Installing packages with requirements

First, create a requirements.txt file inside of the myfirstdjangoapp/ folder, your directory will look like this:

myfirstdjangoapp
├── myvenv
│ └── ...
└───requirements.txt

In your myfirstdjangoapp/requirements.txt file you should add the following text:

djangogirls/requirements.txt

Django~=2.2.4

Now, run pip install -r requirements.txt to install Django.

command-line

(myvenv) ~$ pip install -r requirements.txt
Collecting Django~=2.2.4 (from -r requirements.txt (line 1))
Downloading Django-2.2.4-py3-none-any.whl (7.1MB)
Installing collected packages: Django
Successfully installed Django-2.2.4

That’s it! You’re now (finally) ready to create a Django application!

The first step is to start a new Django project. Basically, this means that we’ll run some scripts provided by Django that will create the skeleton of a Django project for us. This is just a bunch of directories and files that we will use later.

Create project: OS X or Linux

In your Mac OS X or Linux console, you should run the following command. Don’t forget to add the period (or dot) . at the end!

command-line

(myvenv) ~/myfirstdjangoapp$ django-admin startproject mysite .

Create project: Windows

On Windows you should run the following command. (Don’t forget to add the period (or dot) . at the end):

command-line

(myvenv) C:\Users\Name\myfirstdjangoapp> django-admin.exe startproject mysite .

django-admin.py is a script that will create the directories and files for you. You should now have a directory structure which looks like this:

myfirstdjangoapp
├── manage.py
├── mysite
│ ├── __init__.py
│ ├── settings.py
│ ├── urls.py
│ └── wsgi.py
├── myvenv
│ └── ...
└── requirements.txt

Changing settings file

In settings.py, find the line that contains TIME_ZONE and modify it to choose your own timezone. For example:

mysite/settings.py

TIME_ZONE = 'US/Central'

If you want a different language, change the language code by changing the following line:

mysite/settings.py

LANGUAGE_CODE = 'de-ch'

We’ll also need to add a path for static files. (We’ll find out all about static files and CSS later in the tutorial.) Go down to the end of the file, and just underneath the STATIC_URL entry, add a new one called STATIC_ROOT:

mysite/settings.py

STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static')

When DEBUG is True and ALLOWED_HOSTS is empty, the host is validated against ['localhost', '127.0.0.1', '[::1]']. This won't match our hostname on PythonAnywhere once we deploy our application so we will change the following setting:

mysite/settings.py

ALLOWED_HOSTS = ['127.0.0.1', '.pythonanywhere.com']

Set up a database

There’s a lot of different database software that can store data for your site. We’ll use the default one, sqlite3.

This is already set up in this part of your mysite/settings.py file:

mysite/settings.py

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

To create a database for our website, let’s run the following in the console: python manage.py migrate :

command-line

(myvenv) ~/djangogirls$ python manage.py migrate
Operations to perform:
Apply all migrations: auth, admin, contenttypes, sessions
Running migrations:
Rendering model states... DONE
Applying contenttypes.0001_initial... OK
Applying auth.0001_initial... OK
Applying admin.0001_initial... OK
Applying admin.0002_logentry_remove_auto_add... OK
Applying contenttypes.0002_remove_content_type_name... OK
Applying auth.0002_alter_permission_name_max_length... OK
Applying auth.0003_alter_user_email_max_length... OK
Applying auth.0004_alter_user_username_opts... OK
Applying auth.0005_alter_user_last_login_null... OK
Applying auth.0006_require_contenttypes_0002... OK
Applying auth.0007_alter_validators_add_error_messages... OK
Applying auth.0008_alter_user_username_max_length... OK
Applying auth.0009_alter_user_last_name_max_length... OK
Applying sessions.0001_initial... OK

Starting the webserver

You need to be in the directory that contains the manage.py file (the djangogirls directory). In the console, we can start the webserver by running python manage.py runserver:

command-line

(myvenv) ~/djangogirls$ python manage.py runserver

Now you need to check that your website is running. Open your browser and enter this address:

browser

http://127.0.0.1:8000/

You’ve just created your first website and run it using a web server!

--

--

Subba Lakshmi

Full Stack Web Developer | Graduate student of MS in Computer Science at The University of Texas at Rio Grande Valley