0Pricing
Flask Academy · Lesson

URL Prefixes and Blueprint url_for

Namespace routes and link across blueprints.

URL Prefixes and Blueprint url_for is a free Flask 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 Flask Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Group URLs Under a Path

Often every route in a blueprint should sit under one path, like /admin. A url_prefix applies that path to the whole blueprint at once.

Set It at Registration

You pass the prefix when you register the blueprint. Every route inside then lives beneath that shared base path.

app.register_blueprint(admin, url_prefix="/admin")

How Prefixes Combine

A route defined as /users under the prefix /admin becomes /admin/users. Flask simply joins the prefix and the route path.

Keep Routes Prefix-Free

Inside the blueprint, write routes without repeating the prefix. The prefix is set once at registration, so your view paths stay short.

@admin.route("/users")
def users():
    return "Admin users"

Stop Hardcoding Links

Never type URLs as plain strings in templates. url_for builds the correct link from an endpoint name, so links survive path changes.

Endpoints Are Namespaced

With blueprints, an endpoint is blueprint name plus view name. The users view in admin is the endpoint admin.users.

url_for("admin.users")

Prefix Is Applied Automatically

url_for already knows the prefix. Calling it for admin.users returns /admin/users, prefix included, with zero extra work.

Linking Within the Same Blueprint

Inside one blueprint you can use a leading dot as a shortcut. url_for(".users") means the users view in the current blueprint.

url_for(".users")

Linking Across Blueprints

To link from blog to admin, name both parts fully. Always spell out the blueprint when crossing module boundaries.

url_for("admin.dashboard")

Pass Values to Dynamic Routes

Extra keyword arguments to url_for fill dynamic URL parts, so generated links stay correct even for parameterized routes.

url_for("blog.post", post_id=7)

Refactor With Confidence

Change a prefix later and every url_for link updates itself. This is why url_for beats hardcoded strings every single time. 🔗

Quick Check

How do you link to a view inside a blueprint?

Recap

A url_prefix mounts every blueprint route under one path, and url_for builds namespaced links like admin.users that update automatically. 🎉

Frequently asked questions

Is the “URL Prefixes and Blueprint url_for” lesson free?

Yes — the full text of “URL Prefixes and Blueprint url_for” is free to read here on the web, and the Flask 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 Flask Academy course, upgrade to CoddyKit PRO.

What will I learn in “URL Prefixes and Blueprint url_for”?

Namespace routes and link across blueprints. You practise Flask 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 Flask Academy?

No prior experience is required. Flask 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 “URL Prefixes and Blueprint url_for” 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 Flask Academy lesson?

Yes. Every Flask 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. Why Blueprints Beat One Giant File
  2. Create and Register a Blueprint
  3. URL Prefixes and Blueprint url_for
  4. Blueprint-Scoped Templates and Statics
← Back to Flask Academy