Skip to content
Tutorials Center
My learning

Topics

Lesson 66 of 72 ยท Projects

Project: Single-Page Portfolio Site

Build a one-page portfolio with Flask. Use template inheritance and data-driven sections, static CSS, a validated contact form with CSRF and spam protection, and SEO tags.

A personal portfolio is the ideal first "real" site: small enough to finish in an afternoon, useful the moment it is done, and a place to practice nearly everything from the first half of this course. It is one long page with a few sections (a hero, your projects, an about text, a contact form) plus small extras: a stylesheet, a favicon, a page title that search engines like.

You will build it in four steps: a template structure, a data-driven projects section, static files and a contact form that validates input, resists spam, and tells you when someone writes.

flowchart TD
    D["data: PROJECTS list"] --> I["index.html"]
    B["base.html<br/>head, nav, footer"] --> I
    S["static/style.css"] --> B
    I --> P["sections: hero, projects,<br/>about, contact"]
    P -- "POST /contact" --> V["validate + honeypot + CSRF"]
    V -- ok --> M["notify you<br/>(mail or log)"]
    V -- "errors" --> P

One page, but a clean separation: data in Python, structure in templates, looks in CSS.

Project layout

TEXT
portfolio/
  app.py
  forms.py                # the contact form
  content.py              # your projects and bio, as plain Python data
  templates/
    base.html
    index.html
    partials/project_card.html
  static/
    style.css
    favicon.svg
    img/...

(See Project Layout and Static Files.) Keeping content in a Python file means changing a project title never touches HTML. Later it could come from a database or a Markdown folder.

Step 1: base template and data-driven sections

base.html holds everything shared: the <head> with a title block and a meta description, the stylesheet link built with url_for('static', ...) so it works under any URL prefix, the navigation, and the footer. index.html extends it and loops over your data. The cards come from a partial so you can reuse them (Template Inheritance).

The editors here have no files, so the templates and the stylesheet are created in a temporary folder first and Flask is pointed at them. In your project they are ordinary files.

Python Templates, a static stylesheet and sections from data

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Notice the angle brackets in the data (Chat <beta>, <Rust>): Jinja escapes them, so your content can contain anything without breaking the page or opening an XSS hole (Web Security). The context_processor makes owner and year available in every template without passing them in each view (hooks and context).

Style tips that need no framework: a mobile-first layout, a readable max-width, system fonts for speed, scroll-behavior: smooth for in-page links and prefers-color-scheme for a dark theme. If you would like a ready-made look, see Styling Flask Apps with Bootstrap.

Step 2: the contact form

A contact form is where portfolios get attacked: spam bots fill every public form they find. Defend in layers:

  1. CSRF token (Flask-WTF FlaskForm) so other sites cannot submit the form for a visitor.
  2. Validation of every field: required, email format, length limits.
  3. A honeypot field: an input hidden with CSS that people never fill but simple bots do. If it has a value, pretend success and discard the message.
  4. Rate limiting per IP (Flask-Limiter, or your reverse proxy) so one source cannot flood you.
  5. Header safety: never use form text in an email header unchecked (see Flask-Mail).

Then the Post/Redirect/Get pattern: after a successful POST, redirect, flash a thank-you message and show the page again. A refresh must not send the message twice (Flash Messages).

Python A contact form with CSRF, honeypot and Post/Redirect/Get

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Check the last few lines: the valid message produced a redirect, the flash appeared exactly once, the newline in the name was neutralized, and the honeypot submission looked successful to the bot but stored nothing. For production, replace the INBOX list with an email to yourself sent from a background job, or store messages in a table and look at them in Flask-Admin.

Step 3: polish and ship

  • SEO basics: one <h1>, descriptive <title> and meta description (done in base.html), Open Graph tags (og:title, og:image) for link previews, a sitemap.xml and robots.txt as simple routes or static files.
  • Performance: optimize images (use width and height attributes, loading="lazy", modern formats), and let the web server or a CDN serve /static/ with long cache headers (Performance and Caching).
  • Accessibility: a skip link, labels tied to inputs, visible focus, sufficient contrast, meaningful alt text.
  • Error pages: a friendly 404 and 500 (Error Handling).
  • HTTPS and secrets: SECRET_KEY from the environment, secure cookies on HTTPS (Configuration).
  • Deploy: gunicorn behind nginx, or a platform service (Deployment). A site with no database is cheap and easy to host.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Why build the stylesheet link with url_for('static', filename='style.css')?

    url_for generates URLs from the app's configuration, so you never hard-code paths that may change.
  2. 2. How does a honeypot field reduce spam?

    Simple bots fill every input. A value in the hidden field means a bot, so you discard the message.
  3. 3. Why redirect after a successful POST?

    Post/Redirect/Get makes the final page a normal GET, so refreshing it repeats the GET, not the POST.
  4. 4. What does the context_processor in the project provide?

    A context processor returns a dictionary that is merged into every template's context.

Summary

  • Keep content in Python data, structure in templates (base.html, index.html, partials) and looks in static/.
  • Build asset URLs with url_for('static', ...), share variables through context_processor, and let Jinja escape all content.
  • Protect the contact form with CSRF, validation, a honeypot, rate limiting and header-safe handling of text, and use Post/Redirect/Get with a flash message.
  • Add SEO tags, optimized images, accessibility and error pages, then deploy behind a real server.

Next: give the site a polished look quickly with Styling Flask Apps with Bootstrap and Bootstrap-Flask.