What is a server?
A server is a computer located anywhere on the planet that provides computational logic and operations remotely to multiple devices.
What is back-end development?
This is the process of developing software that handles data for a web application. Without the back-end, a website is just static HTML and CSS. Think of a website's back-end as instructions or code that determine how data is rendered in the browser in the form of markup. The back-end contains the logic for processing data obtained by interacting with the website. Back-end development is also known as server-side programming. The back-end of a website is the reason why you can make a post on Facebook, order an item from an e-commerce website, etc. The back-end interacts with a database to store data from a website.
There are various server-side programming languages, including Python, JavaScript (Node.js), Ruby, and Go, among others. These languages can be used to build a web application. Python is one of the best languages for web development because of its simple syntax that almost resembles plain English. Despite its simplicity, it is a very powerful language.
What is a Web Framework?
A web framework is a software that makes building websites easy. Python has many web frameworks, but in this tutorial, we will be looking at two of the most popular.
There are two main frameworks used in Python for building web applications. The first is Django, and the second is Flask. Django is a robust framework with many features, while Flask can be considered a micro framework. Django has a pre-determined structure and pattern of writing web apps, while Flask makes it very simple to get a web application up and running.
The Django Web Framework
With Django, you can build applications that are robust and secure. Its structure makes it easy to build scalable web apps. Django is what we call a full-stack framework; it is structured into the Model-View-Template architecture. Models determine data structure. The model, through the object relation mapper (ORM), lets you query database tables in Python instead of raw SQL. The view handles the logic of the web application, like payment processing, user login/logout, etc., before the HTML is generated with the templates and sent to the client side. Django makes it very easy to work with HTML forms via Django forms. Django comes with its own template engine, which is used for generating web pages.
To start a new django project
- Install django with python package manager (pip)
- Start a new django project
Install django with pip
$ pip install django
$ django-admin startproject django_project
django_project/
|-- django_project/
| |-- __init__.py
| |-- settings.py
| |-- urls.py
| `-- wsgi.py
`-- manage.py
settings
This is were all the configuration of our django application is written. In settings we have our database config, template config, debug mode etc.
URLs
This is our project root URL configuration. This is where we add routes to our Django app. Django comes with a default URL path 'admin/' for Django administration.
from django.urls import path
from django.contrib import admin
from myapp.views import index_view
urlpatterns = [
path('admin/', admin.site.urls), # https://example.com/admin/
path('', index_view) # website home https://example.com
]
WSGI
This is the file responsible for the deployment of our Django application. The wsgi file is the entry point to a Django application used by web servers.
A Django project is sectioned into apps. Apps in Django represent modular parts of a Django project. For example, you can have an app for authentication, and another for a blog, etc.
A Django project structure with app myapp
django_project/
|-- django_project/
| |-- settings.py
| |-- urls.py
| |-- wsgi.py
| `-- __init__.py
|-- myapp/
| |-- migrations/
| |-- admin.py
| |-- tests.py
| |-- apps.py
| |-- models.py
| |-- views.py
| `-- __init__.py
`-- manage.py
Migrations
Migrations are changes made to a database. The migration folder contains all database migrations. Migrations keep track of changes made in a database. Changes made to a database can easily be reverted if the application encounters challenges for any reason.
Admin
Django has a default admin website for managing website content. We register the models that we want to access in the admin in the admin.py file.
Tests
Every good software is often well tested. Django supports test-driven development based on Python's built-in unit testing library. We write tests inside tests.py.
Models
Models are Python objects that represent a database schema. For example, you can have a database table 'books' with columns 'title' and 'description'. A web app's data is managed using models. Every database table must have a primary key column; Django understands this and automatically creates a primary key column for the model.
An example of a django model
from django import models
class Book(models.Model):
class Meta:
db_table = 'books' # table name
title = models.CharField(max_length=255) # column 1
description = models.TextField() # column 2
def __str__(self):
return self.title
Views
This is where our application logic lives. Some other programming languages may call this file a controller. The view handles requests and performs operations based on the data it receives. A Django view is an interface between the database and templates. An example of a simple Django view
from django.shortcuts import render
from django.http import HttpResponse
def index_view(request):
return HttpResponse('Hello, Django') # shows 'Hello, Django' in browser
Flask Web Framework
Flask is a micro framework that has a small and easy-to-extend core. It is easy to learn and use, and it is "beginner-friendly" because it does not have boilerplate code or dependencies like other frameworks. Flask uses Jinja2 for rendering templates. Flask was initially created as an April Fool's joke, but it became very popular quickly, and it is still actively maintained today. We can quickly create a Flask web application in a single Python file 'app.py.'
Flask does connect to databases by default you have to install packages for connecting to a database server.
An example of a flask web application.
from flask import Flask
app = Flask(__name__) # create app
@app.route('/') # register route
def index():
return 'Hello, Flask' # shows 'Hello, Flask' in browser
if __name__ == '__main__':
app.run(debug=True) # run the development server
Conclusion
Django and Flask are both excellent choices for your next project, but if you want to build a small project that does not require much, or you just want to have the flexibility of building web apps however you like, then Flask might be the best option. On the other hand, Django's learning curve may be steep, but once learnt the possibilities are endless because of its rich library of tools that help build large web applications in a short period of time.