Paper 2 Web Applications Drills

These are original Paper 2-style practice questions. They use exact Flask routes, form fields, templates, database schemas, uploads, and expected local evidence.

Detailed answers and model code are in Paper 2 Web Applications Answers.

Revise the topic hub first:

Questions

Question 1: Minimal Route

Create a minimal Flask app with one route /status that returns the text "OK". Include the normal local-server startup block, run the app locally, and state the URL used to test it. [4]

Question 2: Render Template

Create two files:

app.py
templates/hello.html

The route /hello should render templates/hello.html. The template should display Hello Asha using the Jinja variable {{ name }}. Run the app locally and verify the rendered page. [6]

Question 3: Form Input

Create a route /greet that accepts GET and POST.

  • On GET, render an HTML form with an input named name.
  • On POST, read request.form["name"] and display Hello <name>.

Test locally using the name Asha. [8]

Question 4: Validation

Create a route /join that accepts POST. It should read the form field username, strip whitespace, and return "Missing username" if the stripped value is empty. Otherwise return "Joined <username>".

Test both a blank username and Chen. [8]

Question 5: Jinja Loop

Create a route /scores that passes this list to templates/scores.html:

scores = [("Asha", 74), ("Ben", 49), ("Chen", 82)]

The template should display the records as a two-column HTML table with headings Name and Mark, preserving the given order. [7]

Question 6: SQLite Insert

Create a Flask app with a route /add that accepts POST. The route should read form field title, insert it into SQLite table:

Task(id INTEGER PRIMARY KEY AUTOINCREMENT, title TEXT)

The program should create the table if it does not exist, use parameterised SQL, commit the insert, and return "stored". Begin with an empty Task table for this exercise. After posting title=Revise, show evidence that the inserted title can be selected back. [10]

Question 7: SQLite Display

Create a Flask app with route /tasks.

Before testing, set up an isolated Task(id INTEGER PRIMARY KEY AUTOINCREMENT, title TEXT) table containing exactly:

Read
Code

The route should query titles ordered by id and render them in templates/tasks.html as <li> elements. The rendered list should show Read before Code. [10]

Question 8: Static CSS

Create a route /page that renders templates/page.html.

The template should include a stylesheet link using:

{{ url_for('static', filename='style.css') }}

State that style.css belongs in the static/ folder and confirm that the generated page refers to /static/style.css. [5]

Question 9: Upload Handling

Create a Flask route /upload that accepts POST file uploads using field name file.

Provide a small HTML upload form using:

enctype="multipart/form-data"

The route should accept filenames ending in .txt, case-insensitively, and reject missing files, empty filenames, or other extensions. Test notes.txt, NOTES.TXT, image.png, and an empty filename. [8]

Question 10: Test Client

Flask’s test client simulates a request without opening a browser.

Create a Flask app with route /ping returning "pong". Use the test client to print the status code and whether the response body equals "pong". [5]

Review Checklist

After attempting these questions, check whether you can:

  • create Flask routes with exact paths;
  • place templates in templates/;
  • place CSS in static/;
  • read form data from request.form;
  • validate input before processing;
  • use Jinja loops;
  • insert and display SQLite data deterministically;
  • handle uploaded files through request.files;
  • test a web app locally and use a test client for focused route checks.