top of page
Writer's pictureRevanth Reddy Tondapu

Part 2: Building Dynamic URLs with Flask: A Simple Guide


Building Dynamic URLs with Flask
Building Dynamic URLs with Flask

Hello everyone! Today, we're going to explore how to build dynamic URLs using Flask, a popular web framework in Python. This is a fun and important skill to learn because it helps make your web applications more interactive and responsive. Let's dive in and see how it works!


What is Flask?

Flask is a tool that helps you create websites and web applications using Python. It's like a set of building blocks that you can use to put together your own web projects. One of the cool things you can do with Flask is create dynamic URLs, which means the web address can change based on different conditions or inputs.


Setting Up a Simple Flask App

Before we start with dynamic URLs, let's set up a basic Flask app. Here's how you can do it:


Step 1: Install Flask

First, make sure you have Flask installed. You can do this by running the following command in your terminal:

pip install flask

Step 2: Create a Flask App

Open your code editor and create a new file called app.py. Add the following code:

from flask import Flask

app = Flask(__name__)

@app.route('/')
def home():
    return "Welcome to my Flask app!"

if __name__ == '__main__':
    app.run(debug=True)

Step 3: Run Your App

In your terminal, navigate to the folder where app.py is located and run:

python app.py

Step 4: Visit Your App

Open your web browser and go to http://127.0.0.1:5000/. You should see "Welcome to my Flask app!" displayed on the page.


Building Dynamic URLs

Now, let's learn how to create dynamic URLs using Flask's variable rules and URL binding.

Using Variable Rules

Variable rules allow you to capture values from the URL and use them in your functions. Here's an example:

@app.route('/greet/<name>')
def greet(name):
    return f"Hello, {name}!"

Explanation: In this example, <name> is a variable part of the URL. When you visit http://127.0.0.1:5000/greet/Alice, the app will display "Hello, Alice!".


Dynamic URL Example: Pass/Fail Checker

Let's create a simple app that checks if a score is a pass or fail:

@app.route('/result/<int:score>')
def result(score):
    if score >= 50:
        return "Congratulations, you passed!"
    else:
        return "Sorry, you failed. Try again!"

Explanation: Here, <int:score> captures an integer from the URL. If the score is 50 or above, it returns a pass message; otherwise, it returns a fail message.


Redirecting with URL Binding

Sometimes, you might want to redirect users to different pages based on conditions. Here's how you can do it:

from flask import redirect, url_for

@app.route('/check/<int:score>')
def check(score):
    if score >= 50:
        return redirect(url_for('success', score=score))
    else:
        return redirect(url_for('fail', score=score))

@app.route('/success/<int:score>')
def success(score):
    return f"Great job! You passed with a score of {score}."

@app.route('/fail/<int:score>')
def fail(score):
    return f"Oops! You failed with a score of {score}. Better luck next time!"

Explanation: The check function redirects to either the success or fail function based on the score. The url_for function helps create the URL dynamically.


Conclusion

Congratulations! You've learned how to build dynamic URLs using Flask. This skill is useful for creating interactive web applications that respond to user input. As you continue to explore Flask, you'll discover more ways to enhance your web projects.

Stay tuned for more tutorials where we'll dive deeper into integrating HTML, CSS, and JavaScript with Flask. Happy coding!

3 views0 comments

Recent Posts

See All

Comments


bottom of page