0Pricing
Python Academy · Lesson

Routes and Views

Handle HTTP requests.

Routes and Views is a free Python Academy lesson on CoddyKit — lesson 1 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Python Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

What is Flask?

Flask is a lightweight Python web framework. It lets you build web apps and APIs with very little boilerplate. Install it with pip install flask.

from flask import Flask
app = Flask(__name__)
print('Flask app created:', app.name)

Creating the App

Every Flask app starts with an app = Flask(__name__) object. The __name__ argument helps Flask locate resources like templates.

from flask import Flask
app = Flask(__name__)
if __name__ == '__main__':
    # app.run() starts the development server
    print('App is ready to run')

Your First Route

A route maps a URL to a function (a view). The @app.route() decorator binds the path; the function's return value becomes the response body.

from flask import Flask
app = Flask(__name__)

@app.route('/')
def home():
    return 'Hello, world!'
print('Route / -> home')

Multiple Routes

Define as many routes as you like. Each path gets its own view function.

from flask import Flask
app = Flask(__name__)

@app.route('/')
def home():
    return 'Home page'

@app.route('/about')
def about():
    return 'About page'
print('Two routes defined')

Dynamic URL Parts

Capture parts of the URL with <variable>. Flask passes them as arguments to the view.

from flask import Flask
app = Flask(__name__)

@app.route('/user/<username>')
def show_user(username):
    return 'Profile of ' + username
print('URL /user/alice -> show_user')

Typed Converters

Add a converter like <int:post_id> so Flask validates and converts the value to the right type.

from flask import Flask
app = Flask(__name__)

@app.route('/post/<int:post_id>')
def show_post(post_id):
    return 'Post number ' + str(post_id)
print('post_id arrives as an int')

HTTP Methods

By default routes handle GET. Pass methods=['GET', 'POST'] to accept other verbs.

from flask import Flask
app = Flask(__name__)

@app.route('/submit', methods=['GET', 'POST'])
def submit():
    return 'Handled GET or POST'
print('submit accepts GET and POST')

Checking the Method

Inside a view, request.method tells you which verb was used so you can branch your logic.

from flask import Flask, request
app = Flask(__name__)

@app.route('/submit', methods=['GET', 'POST'])
def submit():
    if request.method == 'POST':
        return 'Form submitted'
    return 'Show the form'
print('Branch on request.method')

Returning Status Codes

Return a tuple of (body, status_code) to set the HTTP status, for example 404 for not found.

from flask import Flask
app = Flask(__name__)

@app.route('/missing')
def missing():
    return 'Not here', 404
print('Returns body with status 404')

Building URLs

url_for('view_name') generates a URL from a view's name. This avoids hard-coding paths that might change.

from flask import Flask, url_for
app = Flask(__name__)

@app.route('/about')
def about():
    return 'About'
# url_for('about') -> '/about'
print('url_for builds URLs by view name')

Running the Server

Call app.run(debug=True) to start the development server. Debug mode reloads on changes and shows helpful error pages.

from flask import Flask
app = Flask(__name__)

@app.route('/')
def home():
    return 'Hi'

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

Quick Check

Test your routing knowledge.

Recap

You built Flask routes and views.

  • app = Flask(__name__) creates the app
  • @app.route() maps a URL to a view function
  • <int:id> captures and converts dynamic URL parts
  • methods= and request.method handle HTTP verbs
  • url_for() builds URLs and app.run() starts the server

Frequently asked questions

Is the “Routes and Views” lesson free?

Yes — the full text of “Routes and Views” is free to read here on the web, and the Python Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Python Academy course, upgrade to CoddyKit PRO.

What will I learn in “Routes and Views”?

Handle HTTP requests. You practise Python Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Python Academy?

No prior experience is required. Python Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Routes and Views” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Python Academy lesson?

Yes. Every Python Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Routes and Views
  2. Templates with Jinja2
  3. Forms and Request Data
  4. Building a REST Endpoint
← Back to Python Academy