How do I add arguments to URL in Flask?

How do I add arguments to URL in Flask? FOr example, if I wanted to make a URL for one of my pages, like this: “
Repl link:

def help(topic):
  # Now you have a variable called "topic" that has the same 
  # value as whatever is in the URL. You can use it like a normal
  # variable and use it for if/else operations and render_template
@app.route("/help/<int:topic>") # makes it an integer

How would I set the value of “topic” to a pre-defined Python variable?

I don’t know if you can do that exactly but this would work:

old_topic = "No"
def help(topic):
  topic = old_topic

I don’t see how that helps in any way.

Or do you mean how do you change what topic is? All you need to do is go to the URL and Flask will do the rest. So just include a link to /help/dinosaurs on your website somewhere and then you will have topic = "dinosaur" for whoever uses that URL. The variable is based on the URL, not the other way around.

To redirect a user to a page with a argument do I do return redirect("/cache/<topic>") or return redirect(f"/cache/{topic}")?

The variable “topic” would be pre-defined in both examples

It should be the first one.

The second one. The weird syntax only applies to app.route().


It looks like it should work, but right now when I put input in the search field, it redirects me to the login page instead of the desired redirect that I inputed.

@CoderElijah’s method will work, but it’s not what you’re asking for.

from flask import Flask, ..., request


def some_page():
    level = int(request.args.get("level", default_level))
    # Do something with the level variable

And to redirect a user return redirect(f"/some-page?level={level}")

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.