Flask is a lightweight web framework for Python that allows you to quickly build web applications. In this article, we’ll walk through the process of creating a simple web app using Flask. We’ll cover setting up Flask, defining routes, rendering templates, and handling form data.
Create Contact Us using WTForms in Flask
Installation
First, you need to install Flask. You can do this using pip, the Python package installer:
pip install flask
Getting Started
Create a new directory for your Flask project and navigate into it. Inside the directory, create a new Python file (e.g., app.py) to write your Flask application:
flask import Flask
app = Flask(__name__)
@app.route('/')
def index():
return 'Hello, World!'
if __name__ == '__main__':
app.run(debug=True)
In this example, we import theFlaskclass from theflaskmodule and create a new instance of theFlaskclass. We then define a route using the@app.route()decorator. When a user navigates to the root URL (/), theindex()function is called, which returns the string'Hello, World!'.
Running the Application
To run your Flask application, execute the following command in your terminal:
python app.py
You should see output indicating that the development server is running. Open your web browser and navigate tohttp://localhost:5000to see your Flask app in action.
Rendering Templates
Flask allows you to render HTML templates using the render_template() function. First, create a templates directory inside your project directory. Inside the templates directory, create an HTML file (e.g., index.html):
htmlCopy code<!DOCTYPE html>
<html>
<head>
<title>My Flask App</title>
</head>
<body>
<h1>Hello, Flask!</h1>
</body>
</html>
Update your Flask app to render this template:
from flask import Flask, render_template
app = Flask(__name__)
@app.route('/')
def index():
return render_template('index.html')
if __name__ == '__main__':
app.run(debug=True)
Now, when you navigate to the root URL (/), Flask will render theindex.htmltemplate and display it in your browser.
Handling Form Data
Flask allows you to handle form submissions using the request object. First, create a new HTML file (e.g., form.html) inside the templates directory:
htmlCopy code<!DOCTYPE html>
<html>
<head>
<title>Form Example</title>
</head>
<body>
<form method="POST" action="/submit">
<label for="name">Name:</label>
<input type="text" id="name" name="name">
<input type="submit" value="Submit">
</form>
</body>
</html>
Update your Flask app to handle form submissions:
from flask import Flask, render_template, request
app = Flask(__name__)
@app.route('/')
def index():
return render_template('form.html')
@app.route('/submit', methods=['POST'])
def submit():
name = request.form.get('name')
return f'Hello, {name}!'
if __name__ == '__main__':
app.run(debug=True)
Now, when you navigate to the root URL (/), you’ll see a form where you can enter your name. After submitting the form, Flask will display a personalized greeting message.
How does the output looks like ?
- Index Page (/):
- When you navigate to the root URL (
/), you will see theindex.htmltemplate rendered in your browser. - The page will display a heading saying “Hello, Flask!”.
- When you navigate to the root URL (
- Form Page (/submit):
- The form page (
/) will display a form with a single input field for entering your name. - After submitting the form, Flask will process the form data and display a personalized greeting message on the page, such as “Hello, John!” if you entered “John” in the form.
- The form page (
Conclusion
Flask is a powerful and flexible framework for building web applications in Python. In this article, we’ve covered the basics of creating a Flask application, defining routes, rendering templates, and handling form data. With Flask, you can quickly create web applications for a variety of purposes, from simple prototypes to full-fledged web services.





Leave a Reply