Skip to content
Advertisement

Why doesn’t Flask app enter GET method but does enter POST methods?

I apologize, this has to be a duplicate but I haven’t found anything that’s helped, so I imagine it must be an import or syntax issue? Also, I’m on crash course levels of Python and HTML here, and it’s finals week, so bear with me.

I’m working on a homework assignment that specifies a HTML page must use a form with the GET method and send its data back to the original location “assignment10.html”. The app never enters the function that uses the form being submitted with GET, just reloads the page after submitting (url has query string). The app will enter the function if I use POST as the method in the form and function parameter and use request.form.get(). If it matters at all, I’m using a virtual machine with Ubuntu 64-bit

I’ve searched around, but most of the answers deal with json or ajax or some other software, but because this is an assignment, I’m limited to the python file and a templates folder for flask.

I’ve played around a little with the request object, using request.form.get() and request.args.get() but that’s not the obstacle here, as far as I can tell. I’m stuck at this point and I need to know what’s my mistake.

# assignment10.py
from flask import Flask, render_template, request
app = Flask(__name__)

@app.route('/assignment10.html')
def start():
    return render_template('Start.html')

@app.route('/assignment10.html', methods=['GET']) #Enters if 'POST' instead
def next():
    return "Entered next()"  # just to test function is entered
    fname = request.args.get('fname') #change to .form.get()
    lname = request.args.get('lname') # "                 "
    # render next template
<!-- Start.html -->
<!DOCTYPE html>
<html>
<head>
<title> Assignment 10 </title>
<script>
    function validate(){
        <!-- if fname && lname are not blank return true else false -->
     }
</script>
</head>

<body>
<!-- switch to method="POST" works -->
<form action = "assignment10.html" method="GET" onsubmit="return validate()">
    Your first name:
    <input type="text" name="fname" id="fname" value="First"><br>
    Your last name:
    <input type="text" name="lname" id="lname" value="Last"><br>

    <input type="submit" value="Submit">
</form>
<div id="msg"></div>
</body>
</html>

Advertisement

Answer

@app.route('/assignment10.html')
def start():
    fname = request.args.get('fname') #change to .form.get()
    lname = request.args.get('lname') # "                 "
    print(fname, lname)
    return render_template('./Start.html')

You need only one route for your requirement.
First request, fname and lname is None

why ‘GET’ not working?:
@app.route('/assignment10.html', methods=['GET'])
@app.route('/assignment10.html')

The two route is equal.
Because of @app.route ‘s default methodsis ['GET']

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement