How a website works

Simple syntax, a powerful ecosystem and mature frameworks make Python a convenient language to start web development with.

Just starting out? First go through the “From scratch” lessons, and look up unfamiliar words in the glossary.

Operators at an international Bell System switchboard connecting calls

What backend development is

The backend is the part of an application that runs on the server: it receives requests, handles the logic, talks to the database and sends a response.

Handling requests

The framework receives an HTTP request and decides which code should handle it.

Database

The application reads and saves users, products, articles and other data through an ORM or SQL.

Building the response

The server returns HTML, JSON or a file: what the user finally sees.

Logic and security

Checking data, access rights, handling errors and preparing a correct response.

What Python does on the server

The server receives requests, works with data and returns responses. This is what Python code does for that.

  • Matches the page address to the right function (routing).
  • Checks the data the user sent, for example from a form.
  • Reads and writes data in the database.
  • Checks access rights and user logins.
  • Builds HTML pages from templates.
  • Gives data in JSON format to other programs.

How a user's request travels

A simplified chain from the click to the server's response.

1

Browser

The user opens a page or submits a form.

2

HTTP request

The request goes to the server: method, address and data.

3

Framework

Django or Flask finds the right route and handler.

4

Logic and DB

The code checks the data and reads or saves information.

5

Response

The server returns a page, JSON or an error.

The request's path step by step

What happens in a split second after you press the “Add task” button.

  1. The browser sends a request. The form becomes a POST /tasks request with headers, cookies and the form data.
  2. The web server receives it. It terminates the HTTPS connection, serves static files itself and passes the rest to the application through WSGI.
  3. The framework finds the route. The address /tasks and the POST method point to your function, which receives the request object.
  4. The function checks the data. Is the user logged in, is there a CSRF token, is the title empty? On an error it returns a 400 or 403 response right away.
  5. The database saves the record. An INSERT INTO tasks ... with parameters runs, and the database returns the new row's number.
  6. The application prepares the response. This is a redirect to the list, HTML from a template or JSON for an API.
  7. The web server sends the response. The status code, headers and body travel back over the network.
  8. The browser shows the result. It follows the redirect, builds the page and loads the CSS, JavaScript and images.

Python's strengths on the web

The main reasons the language stays a popular choice for server-side development.

Why Python is good for starting web development
Feature Why it matters
Readable syntax Code is easier to maintain and review in a team, and beginners get in more easily.
Ready-made frameworks Django and Flask cover most typical server-side tasks.
Library ecosystem It is easy to add a database, authentication, API tools and analytics.
Link to data analysis One language can be used for the web, automation and data analysis.

Code examples

A minimal Flask server and basic work with SQLite: two important entry points.

Flask: a minimal server
from flask import Flask

app = Flask(__name__)

@app.route("/")
def home():
    return "Hello, this is a Python server!"

if __name__ == "__main__":
    app.run()
SQLite: basic work with data
import sqlite3

conn = sqlite3.connect("app.db")
cursor = conn.cursor()

cursor.execute(
    "CREATE TABLE IF NOT EXISTS users (id INTEGER PRIMARY KEY, name TEXT)"
)

cursor.execute(
    "INSERT INTO users (name) VALUES ('Aigerim')"
)

conn.commit()
conn.close()

Note: the examples are for learning, and no server runs in this project. Here you can only read and copy the code.

A mini glossary

Terms that help you read web development material with more confidence.

Backend

The server side of an application, which handles requests, stores data and returns responses.

Frontend

The client side of a website: the interface, buttons, forms and everything the user sees in the browser.

HTTP

The protocol the browser and server use to exchange requests and responses.

ORM

A tool that lets you work with a database through objects and code instead of hand-written SQL queries.

API

An interface through which one program can get data or functions from another program.

SQLite

A lightweight database often used for learning and small projects.

Practical checklist

What you can do after studying this section.

  • Explain how a server differs from a browser.
  • Understand why an application needs a database.
  • Start a simple Flask server.
  • Create one route and return some text.
  • Make a table in SQLite and add a record to it.
  • Compare which tasks are better solved with a framework than by hand.

Test yourself

Four questions on this section. An explanation appears after you answer.

1. Where does the backend code of a website run?
2. What does routing do in a framework?
3. In what format does a server usually give data to other programs?
4. What happens first when a user opens a page?

Sources

Checked on . The examples are for Python 3.12 and newer.

Where to go next?

Once the basics are clear, the next step is to choose a framework: Django or Flask.

How it all connects

You open a page, the browser sends a request to the server, a Python program finds the data and sends back a response. Below is the same as a list.

Diagram: the browser sends a request to the server, the Python server uses Django or Flask, queries the database and returns the page to the user.
  • Python
    • The server side of a website (backend)
      • Handling requests
      • Working with a database
      • Building the response
      • Which address opens which page
    • Django
      • Admin panel and data handling
      • A ready project structure
      • A quick start for sites with records
    • Flask
      • Only the essentials
      • Everything else is added as needed
      • Good for small services