How to make your first Python and Django Web App

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.

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!

$ mkdir myfirstdjangoapp
$ cd myfirstdjangoapp
$ python3 -m venv myvenv
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).

C:\Users\Name\myfirstdjangoapp> myvenv\Scripts\activate
$ source myvenv/bin/activate

Installing Django

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

(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
Django~=2.2.4
(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
(myvenv) ~/myfirstdjangoapp$ django-admin startproject mysite .
(myvenv) C:\Users\Name\myfirstdjangoapp> django-admin.exe startproject mysite .
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:

TIME_ZONE = 'US/Central'
LANGUAGE_CODE = 'de-ch'
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
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.

DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
}
}
(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:

(myvenv) ~/djangogirls$ python manage.py runserver
http://127.0.0.1:8000/

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

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store