Flask Programming

Hands On No. 2 : Working with Forms

Resources

Source Code of hello.py
from flask import Flask
from flask import render_template
from flask import request

app = Flask(__name__, template_folder='templates')

@app.route("/")
def hello():
     return render_template('index.html')

@app.route("/index.html")
def hello1():
     return render_template('index.html')

@app.route("/aboutus")
def aboutus():
    return render_template('aboutus.html')

    @app.route("/contactus")
def contactus():
    return render_template('contactus.html')

@app.route("/data-input")
def datainput():
    return render_template('Data-Input.html')

@app.route('/viewdata', methods=['GET', 'POST'])
def home():
    if request.method == 'POST':
        fullname = request.form.get('fname')
        age = request.form.get('age')
        
        return f'{fullname}, your age is {age}'
    return render_template('Data-Input.html')

@app.route('/viewdata1', methods=['GET', 'POST'])
def home1():
    if request.method == 'POST':
        fullname = request.form.get('fname')
        age = request.form.get('age')
        return render_template('Display-Data.html', fullname=fullname,age=age)
    

@app.route('/viewdata2', methods=['GET', 'POST'])
def home2():
    if request.method == 'POST':
        fullname = request.form.get('fname')
        age = request.form.get('age')
        return render_template('Display-Data1.html', fullname=fullname,age=int(age))
    if request.method == 'GET':
        return render_template('get-is-not-supported.html')

Source Code of index.html
Welcome to Home Page

Source Code of aboutus.html
Welcome to About Us Page

Source Code of contactus.html
Welcome to Contact Us Page

Source Code of Data-Input.html

<!DOCTYPE html>
<html>
<head>
<title>Personal Details</title>
<script>

function dataCheck()
{
var a = document.getElementById("fname").value.length;
if(a==0)
{
alert("Enter Your Name ");
return false;
}
var b = document.getElementById("age").value.length;
if(b==0)
{
alert("Enter Your Age ");
return false;
}
}
</script>
</head>
<body>

<form name="frmmain" action="viewdata2" method="POST">
<label for="fname">Enter Name :</label><br>
<input type="text" id="fname" name="fname" value=""><br>
<label for="lname">Enter Your Age:</label><br>
<input type="text" id="age" name="age" value=""><br><br>
<input type="submit" value="Submit" onclick="return dataCheck()" onsubmit="return dataCheck()">
</form>
</body>
</html>

Source Code of Display-Data.html

<!DOCTYPE html>
<html>
<head>
<title>Personal Details</title>
</head>
<body>
<h1>Your name is {{ fullname }} <br>Your age is {{ age }}</h1>
</body>
</html>

Source Code of Display-Data1.html

<!DOCTYPE html>
<html>
<head>
<title>Personal Details</title>

</head>
<body>
<h1>You name is {{ fullname }}</h1>
{% block body %}
{% if age > 18 %}

<p style="color: green; font-size: 30px;"> Congratulations!! You are Elligible for Voting </p>

{% else %}

<p style="color: red; font-size: 30px;"> Sorry !! You can not Vote </p>

{% endif %}
{% endblock %}
</body>
</html>

Source Code of get-is-not-supported.html
Get method support is not available