0Pricing
Python Academy · Lesson

Forms and Request Data

Process user input.

Forms and Request Data is a free Python Academy lesson on CoddyKit — lesson 3 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.

The request Object

Flask gives every view access to a global request object holding all the incoming data: form fields, query strings, JSON, headers, and more.

from flask import Flask, request
app = Flask(__name__)
# request is available inside any view
print('request carries the incoming data')

Query String Parameters

Data after ? in the URL lives in request.args. Use .get() to read a value safely.

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

@app.route('/search')
def search():
    term = request.args.get('q', '')
    return 'Searching for ' + term
print('/search?q=cats -> request.args')

An HTML Form

A form sends data when submitted. The method and action attributes decide how and where.

# <form method='POST' action='/login'>
#   <input name='username'>
#   <input name='password' type='password'>
#   <button>Submit</button>
# </form>
print('Form posts username and password')

Reading Form Data

Submitted form fields arrive in request.form. Access them by the input's name attribute.

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

@app.route('/login', methods=['POST'])
def login():
    user = request.form['username']
    return 'Welcome ' + user
print('request.form holds posted fields')

Safe Access with get

Using request.form['x'] raises if the key is missing. Prefer request.form.get('x'), which returns None instead.

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

@app.route('/login', methods=['POST'])
def login():
    user = request.form.get('username')
    if not user:
        return 'Missing username', 400
    return 'Welcome ' + user
print('.get avoids KeyError')

GET and POST in One View

A common pattern: show the form on GET, process the data on POST, all in one view.

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

@app.route('/contact', methods=['GET', 'POST'])
def contact():
    if request.method == 'POST':
        return 'Thanks, ' + request.form.get('name', 'friend')
    return render_template('contact.html')
print('One view, two methods')

Validating Input

Never trust user input. Check that required fields exist and have sensible values before using them.

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

@app.route('/age', methods=['POST'])
def age():
    raw = request.form.get('age', '')
    if not raw.isdigit():
        return 'Age must be a number', 400
    return 'Age is ' + raw
print('Validate before trusting input')

Reading JSON

API clients often send JSON. request.get_json() parses the body into a Python dict.

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

@app.route('/api', methods=['POST'])
def api():
    data = request.get_json()
    return 'Got name ' + data.get('name', '?')
print('get_json parses the JSON body')

File Uploads

Uploaded files live in request.files. Save one with its .save() method after validating it.

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

@app.route('/upload', methods=['POST'])
def upload():
    f = request.files.get('photo')
    if f:
        return 'Received ' + f.filename
    return 'No file', 400
print('request.files holds uploads')

Redirect After POST

After handling a form, redirect the user to avoid duplicate submissions on refresh. Use redirect(url_for(...)).

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

@app.route('/done')
def done():
    return 'All done'

@app.route('/save', methods=['POST'])
def save():
    return redirect(url_for('done'))
print('Post/Redirect/Get pattern')

Flash Messages

flash() stores a one-time message shown after a redirect, great for confirmations like 'Saved successfully'.

from flask import Flask, flash, redirect, url_for
app = Flask(__name__)
app.secret_key = 'change-me'

@app.route('/save', methods=['POST'])
def save():
    flash('Saved successfully')
    return redirect(url_for('save'))
print('flash shows a one-time message')

Quick Check

Test your form-handling knowledge.

Recap

You processed user input in Flask.

  • request.args for query strings, request.form for form fields
  • request.get_json() for JSON bodies, request.files for uploads
  • Always validate input and use .get() for safe access
  • Use Post/Redirect/Get and flash() for clean UX

Frequently asked questions

Is the “Forms and Request Data” lesson free?

Yes — the full text of “Forms and Request Data” 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 “Forms and Request Data”?

Process user input. 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 3 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Forms and Request Data” 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