Code

from flask import Flask, render_template, request, session
# First we imported the Flask class.

app = Flask(__name__) # Next we create an instance of this class.
app.secret_key = '_5#y2L"F4Q_MY_SESSION_KEY_8z\\n\\xec]/'
# Set the secret key to some random bytes.

@app.route("/", methods=['GET', 'POST'])  # Decorator
def home():
    if 'user_id' in session:
        user_id = session['user_id']
        return render_template('home.html', user_id=user_id)
    else:
        return render_template('home.html')
# We use the route() decorator to tell Flask what URL should trigger our function.

# GET
@app.route('/search', methods=['GET', 'POST'])
def search():
    search = request.args.get('search')
    return render_template('search.html', search=search)

# POST
@app.route('/login', methods=['GET', 'POST'])
def login():
    if request.method == 'POST':
        session['user_id'] = request.form['user_id']
        user_id = session['user_id']
        return render_template('home.html', user_id=user_id)
    else:
        return render_template('login.html')

if __name__ == '__main__':
# This conditional statement is used to distinguish
# when a script file is used as a main program and when used as a module.
    app.run()

GET

The content is encoded in the URL’s query string when the submission method is GET.

In this case, the browser sends an empty body. Because the body is empty, if a form is sent using this method the data sent to the server is appended to the URL.

<form action="/search" method="GET">
		<input type="text" name="KEY" />
		<!-- value is VALUE-->

The data is appended to the URL as a series of name/value pairs.

def search():
    search = request.args.get('KEY')
		# The form of arguments
		search = request.args['KEY']

After the URL web address has ended, we include a question mark (?) followed by the name/value pairs, each one separated by an ampersand (&). In this case, we are passing two pieces of data to the server. If you are aware of the name, you can retrieve the value directly.

POST

It's the method the browser uses to talk to the server when asking for a response that takes into account the data provided in the body of the HTTP request.

<form action="/login" method="POST">
    <input type="text" name="user_id" />
		<input type="password" name="user_pwd" />
		<input type="submit" value="login" />

If a form is sent using this method, the data is appended to the body of the HTTP request.

def result():
    user_id = request.form['user_id']
		user_pwd = request.form['user_pwd']
    return render_template('result.html', user_id=user_id, user_pwd=user_pwd)
		# == return render_template('result.html', user_id=form['user_id'])

Session

In order to use sessions you have to set a secret key.