Obafemi Emmanuel

Web Development with Python

Published 1 month ago

Python is one of the most versatile and popular programming languages, widely used in web development. Two of the most prominent web frameworks for Python are Flask and Django. This guide will introduce you to both frameworks, helping you understand their differences and learn how to build web applications using them.


1. Introduction to Web Frameworks (Flask & Django)

What is a Web Framework?

A web framework is a collection of tools and libraries that simplifies the process of building web applications. It provides a structured way to handle requests, responses, database interactions, and security concerns.


Flask: A Lightweight Microframework

Flask is a minimalist, micro-framework for Python that is highly flexible and simple to use. It is best suited for small to medium-sized applications and APIs. Flask follows a “barebones” approach, giving developers complete control over which components they want to use.

Key Features of Flask:

  • Lightweight and modular
  • Minimal setup and configuration
  • Integrated development server and debugger
  • Jinja2 templating engine
  • Extension support for database integration, authentication, and more

Django: The Full-Stack Web Framework

Django is a high-level web framework that follows the “batteries-included” philosophy, meaning it provides everything you need to build a full-fledged web application. It is ideal for larger applications where rapid development and maintainability are essential.


Key Features of Django:

  • Built-in ORM (Object-Relational Mapping) for database management
  • Secure authentication system
  • Prebuilt admin interface
  • Scalable and secure
  • Encourages best practices with the DRY (Don’t Repeat Yourself) principle

2. Building a Simple Web App with Flask

Let's start by creating a basic web application using Flask.


Step 1: Install Flask

First, ensure you have Python installed, then install Flask using pip:

pip install flask

Step 2: Create a Flask Application

Create a new Python file (e.g., app.py) and add the following code:

from flask import Flask

app = Flask(__name__)

@app.route('/')
def home():
    return "Hello, Flask! Welcome to Web Development."

if __name__ == '__main__':
    app.run(debug=True)

Step 3: Run the Flask Application

Run the script in your terminal:

python app.py

You should see output indicating that the server is running. Open your browser and go to http://127.0.0.1:5000/ to view your web app.


Step 4: Adding Templates

Flask uses Jinja2 for templating. Create a templates folder and add an index.html file inside it:

<!DOCTYPE html>
<html>
<head>
    <title>Flask App</title>
</head>
<body>
    <h1>Welcome to Flask Web Development</h1>
</body>
</html>

Modify app.py to render this template:

from flask import Flask, render_template

app = Flask(__name__)

@app.route('/')
def home():
    return render_template('index.html')

if __name__ == '__main__':
    app.run(debug=True)

Restart the server, and you’ll see the rendered template.


3. Building a Full-Stack Application with Django

Step 1: Install Django

Install Django using pip:

pip install django

Step 2: Create a Django Project

Run the following command to create a new project:

django-admin startproject myproject

Navigate into your project folder and run the server:

cd myproject
python manage.py runserver

Visit http://127.0.0.1:8000/ to see the default Django welcome page.


Step 3: Create a Django App

Inside the project directory, create a new app:

python manage.py startapp myapp

Register this app in settings.py under INSTALLED_APPS:

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'myapp',
]

Step 4: Create a View

Edit myapp/views.py:

from django.http import HttpResponse

def home(request):
    return HttpResponse("Hello, Django! Welcome to Web Development.")

Step 5: Configure URLs

Edit myapp/urls.py (create the file if it doesn’t exist):

from django.urls import path
from . import views

urlpatterns = [
    path('', views.home, name='home'),
]

Also, include this app’s URLs in the main urls.py:

from django.contrib import admin
from django.urls import include, path

urlpatterns = [
    path('admin/', admin.site.urls),
    path('', include('myapp.urls')),
]

Step 6: Run the Server

python manage.py runserver

Visit http://127.0.0.1:8000/ to see your Django app in action.


Step 7: Create a Template

Django uses its own templating engine. Create a templates folder inside myapp and add index.html:

<!DOCTYPE html>
<html>
<head>
    <title>Django App</title>
</head>
<body>
    <h1>Welcome to Django Web Development</h1>
</body>
</html>

Modify views.py to render this template:

from django.shortcuts import render

def home(request):
    return render(request, 'index.html')

Restart the server, and you’ll see the updated output.


Conclusion

Both Flask and Django are excellent web frameworks with different strengths. Flask is ideal for small projects and APIs where flexibility is required, while Django is great for large applications requiring built-in features. By learning both, you can choose the right tool for each project and build scalable, efficient web applications with Python.


Leave a Comment


Choose Colour