0

I'm new to python and I am getting the error listed above

@app.route('/AddNewInfo', methods = ['GET', 'POST'])
def AddNewInfo():
    if request.method == 'POST':
        name = request.form['name']
        email = request.form['email']
        submit = request.form['submit']
        if submit:
            with open("info.txt", "w") as text_file:
                text_file.write((name, email))
        return render_template('show_info.html',name=name, email=email)
    else:
        return render_template('AddNewInfo.html')

HTML:

<html>
 <head>
   <title>Add new information</title>
 </head>
 <body>
 <h1>Enter New Info</h1>
        <form action="" method="post">
            Name: <input type="text" name="name"><br />
            Email: <input type="text" name="email"><br />
            <input type="submit" value="Submit">
        </form>
   </body>

</html>

It is something to do with if request.method == 'POST' method as it works if it is only returning AddNewInfo

FIXED Had a post request instead of get in my index.html

Sue
  • 11
  • 1
  • 4
  • Presumably this is in Flask? – Martijn Pieters Mar 28 '15 at 14:25
  • Do you get any traceback on the console? – Martijn Pieters Mar 28 '15 at 14:51
  • 127.0.0.1 - - [28/Mar/2015 14:55:45] "GET / HTTP/1.1" 200 - 127.0.0.1 - - [28/Mar/2015 14:55:49] "POST /AddNewInfo HTTP/1.1" 400 - 127.0.0.1 - - [28/Mar/2015 14:57:31] "POST /ViewAllInfo HTTP/1.1" 200 - 127.0.0.1 - - [28/Mar/2015 14:57:34] "POST /AddNewInfo HTTP/1.1" 400 - 127.0.0.1 - - [28/Mar/2015 14:57:36] "POST /UpdateExistingInfo HTTP/1.1" 200 127.0.0.1 - - [28/Mar/2015 14:57:38] "POST /DeleteInfo HTTP/1.1" 200 - 127.0.0.1 - - [28/Mar/2015 14:57:40] "POST /SpecificInfo HTTP/1.1" 200 - 127.0.0.1 - - [28/Mar/2015 14:57:42] "POST /SortInfo HTTP/1.1" 200 - – Sue Mar 28 '15 at 14:58
  • That means the `request.form` object doesn't have a key you are looking for. Perhaps the `submit` button wasn't sent along, for example. – Martijn Pieters Mar 28 '15 at 14:59
  • Right, you are getting a 400 error, because one of the keys you tried to doesn't exist in the POST request. Your `submit` button has no `name` attribute so it'll not be part of the request. – Martijn Pieters Mar 28 '15 at 15:04
  • It still thinks name and email don't either? – Sue Mar 28 '15 at 15:31

0 Answers0