2

I just started learning Flask, and as a practice project I wanted to build a simple site that asks the user for their name, and greets them by their name on a new page. I have been unable to get a user's name through a form, and display it on a new page due to to a 'Bad Request' error. My code is below.

This is my index page with the form on it:

<!DOCTYPE html>
<html>
<head>
    <title>Index</title>
</head>
<body>
    <h1>Practice index page</h1>
    <h2>Welcome to my practice web page.</h2>   

    <form action = "/firstname">
        <p>What's your name?</p>
        <input type = "text" name = "yourname"><br>
        <input type = "submit" value = "Submit">
    </form>
</body>
</html>

This is my application.py file:

from flask import Flask
from flask import render_template, request, redirect
app = Flask(__name__)

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

@app.route('/firstname')
def first_name():
    yourname = request.form['yourname']
    return render_template('firstname.html', name = yourname)

And this is my firstname.html file:

<!DOCTYPE html>
<head>
    <title>My name is</title>
</head>
<body>
    <h1>Hello</h1>
    <h2>Your name is {{name}}.</h2>
</body>

The index page loads fine. The firstname.html template also loads fine when the user's name is hardcoded, it's only when I get it from the form that problems arise.

I have been at this for a few hours, watched YT videos, Googled like crazy, and still can't figure out what's wrong, so I would really appreciate some help!

3 Answers 3

2

By default, a Flask route only answers to GET requests. You can tell the first_name view to answer both GET and POST requests like so:

@app.route('/firstname', methods=['GET', 'POST'])
def first_name():
    yourname = request.form['yourname']
    return render_template('firstname.html', name = yourname)

You also need to set the form method to POST so that yourname is sent as form data (readable in request.form) and not as a URL parameter (readable in request.args).

<form action = "/firstname" method="POST">
    <p>What's your name?</p>
    <input type = "text" name = "yourname"><br>
    <input type = "submit" value = "Submit">
</form>
Sign up to request clarification or add additional context in comments.

3 Comments

I thought this might be the issue but including POST in the methods isn't fixing the problem either. I'm still getting the 'Bad Request' error. Honestly at wits' end :(
That's weird. Did you also edit the form method? Flask can raise a Bad Request error if yourname was not found in request.form.
Oh, I completely forgot to change the form method. All seems to work fine now, thank you!
1

Use request.args['yourname'] instead of request.form['yourname']

Your index.html form is calling /firstname url with get method and name argument as query string

GET /firstname?yourname=Sunny HTTP/1.1

so you need to access query parameters with request.args['yourname'] & not with request.form['yourname']

Comments

0

You need to pass variables as dict and not directly.

Like this

@app.route('/firstname')
def first_name():
    yourname = request.form['yourname']
    return render_template('firstname.html', **{"name": "yourname"})

1 Comment

This is equivalent to passing yourname as a keyword argument.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.