Getting Started with Python Django Web Development
Python Django is one of the most commonly used web development platforms. It offers a simplified approach to web development and is easy to follow, even for beginners. Let us get started with Python Django Web Development.
What is a Website?
A website is basic HTML code, that is decoded by your browser to display — text, images, animations, videos. A collection of information that was intended to be displayed by the website developer.
This HTML code, sits in a web hosting server — requested by IP addresses all across the world.
Static vs Dynamic web content
As a developer, the simplest type of website to build is a static website. This is similar to a powerpoint presentation or word document, the code is written once and is static — the same code is displayed to everyone who requests it. Types of these websites include: (1) Profile website, (2) Company website, (3) Listing your services online etc. These types of websites usually do not require a user profile management system (logging in and logging out) or a database.
Dynamic websites on the other side are complex web applications that display dynamic content. Two people requesting the same website will receive two different sets of HTML files and responses. A great example is Facebook or Twitter. If I log in to facebook, my homepage will look completely unique. The posts and images I see, depend on my likes, friends I have and pages I am following.
These types of websites usually require a user management system — authentication. The application needs to know, who I am , so it can display the relevant data to me. Also, I need to be authenticated so I do not see data I am not entitled to see.
Enter Django
Django is specifically used to create dynamic web applications with complex data structures, authentication and front ends. Django was first created in 2005 and has since grown to be one of Pythons most preferred and widely used web application frameworks.
Great, well known brands have used Django to build their own web application. Organisations like Spotify, NASA, Washington Post, Quora, Pinterest and YouTube (The biggest video sharing platform in the world) are powered by Python Django. There are many more: See — top 19 websites powered by Django.
Getting Started with Python Django Web Development
We are going to be working from Ubuntu OS — you can get your own Virtual Server and set it up for ubuntu for $5 — Get Virtual Server Droplet from Digital Ocean.
Check this Tutorial for setting up your VPS:https://www.youtube.com/watch?v=Zyl23djnsVg
We are also going to go straight for a Postgres Database, ready for deployment in your virtual server
Install Python 3
sudo apt-get update
sudo apt-get install python3-pip python3-dev libpq-dev postgresql postgresql-contrib
Create and Set-Up Database
sudo -u postgres psql
Inside the Postgres Prompt — Remember to replace skolo with your database name.
CREATE DATABASE skolo;
CREATE USER skolouser WITH PASSWORD 'password';
ALTER ROLE skolouser SET client_encoding TO 'utf8';
ALTER ROLE skolouser SET default_transaction_isolation TO 'read committed';
ALTER ROLE skolouser SET timezone TO 'UTC';GRANT ALL PRIVILEGES ON DATABASE skolo TO skolouser;
\q
Set-up Virtual Environment and Django Project
sudo -H pip3 install --upgrade pip
sudo -H pip3 install virtualenv
Create a directory for your project
mkdir skolo && cd skolo
virtualenv skoloenvsource skoloenv/bin/activate
Now your venv has been activated, you can start installing your python packages
pip install django
pip install psycopg2
Now tha Django has been installed in our virtual environment — we can use it to create a Django project.
django-admin startproject skoloapp
Django will create for you all the filed required to initiate a project, as seen in the file structure below:
skolo/
│
├── skoloapp/
│ ├── skoloapp/
│ │ ├── __init__.py
│ │ ├── settings.py
│ │ ├── urls.py
│ │ └── wsgi.py
│ │
│ └── manage.py
│
└── skoloenv/
At this stage, you need to open the settings.py file and make some changes to it.
cd skoloapp
You are now in the root of your project — I prefer to run my code from here, to edit the settings.py file type
nano skoloapp/settings.py
Scroll down, look for ‘ALLOWED_HOSTS’ and enter your VPS IP address in the list, including ‘localhost’.
Scroll down look for DATABASES — to enter the database settings: Remember to replace skolo with the project name you used when creating the database.
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'skolo',
'USER': 'skolouser',
'PASSWORD': 'password',
'HOST': 'localhost',
'PORT': '',
}
}
At the bottom of the File, enter:
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static/')
Run the Django App
You are now ready to run the “out of the box” Django app. You need to make sure you are in the base of the app, where the manage.py file is.
Start by running migrations to set up database.
python manage.py makemigrations
python manage.py migrate
Create an admin user that you can log in to the admin panel
python manage.py createsuperuser
Create static folder
python manage.py collectstatic
You can now run the app on port 5000 for example: Make sure the firewall is down on port 5000
sudo ufw allow 5000python manage.py runserver 0.0.0.0:5000
Visit your front end of the serve, port 5000 to see your app.
Getting started with Python Django Full Video Series on YouTube — Over 5hours of Python Django Development, and new videos added weekly until the series is complete.