0

I have simple python app which gets name and phone from html form submit using post method. I have this in my index.html:

<form action="http://127.0.0.1:5000/get_phone/" method = "POST">
First name:
<input type="text" name="firstname" >
Phone:
<input type="text" name="lastname" >
<input type="submit" value="Submit">
</form>

And have my getPhone.py:

from flask import Flask
app = Flask(__name__)
@app.route('/get_phone/', methods=['POST', 'GET'])
def get_phone():
 if request.method == 'POST':
 print ('First name:', request.form['firstname'])
 print ('Phone:', request.form['lastname'])
 return 'Take a look at your terminal!'
if __name__ == '__main__':
 app.run()

But when I submit my form with I get the following message in my browser:

Internal Server Error

The server encountered an internal error and was unable to complete your request. Either the server is overloaded or there is an error in the application.

In my console I have this:

> * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
127.0.0.1 - - [04/Feb/2015 22:20:04] "GET /get_phone/ HTTP/1.1" 500 -
127.0.0.1 - - [04/Feb/2015 22:20:12] "POST /get_phone/ HTTP/1.1" 500 -
127.0.0.1 - - [04/Feb/2015 22:26:22] "GET /get_phone/ HTTP/1.1" 500 -

How to fix this?

UPD: consoleLog with debug = true:

 * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
 * Restarting with stat
127.0.0.1 - - [04/Feb/2015 22:35:30] "POST /get_phone/ HTTP/1.1" 500 -
Traceback (most recent call last):
 File "C:\Python34\lib\site-packages\flask\app.py", line 1836, in __call__
 return self.wsgi_app(environ, start_response)
 File "C:\Python34\lib\site-packages\flask\app.py", line 1820, in wsgi_app
 response = self.make_response(self.handle_exception(e))
 File "C:\Python34\lib\site-packages\flask\app.py", line 1403, in handle_except
ion
 reraise(exc_type, exc_value, tb)
 File "C:\Python34\lib\site-packages\flask\_compat.py", line 33, in reraise
 raise value
 File "C:\Python34\lib\site-packages\flask\app.py", line 1817, in wsgi_app
 response = self.full_dispatch_request()
 File "C:\Python34\lib\site-packages\flask\app.py", line 1477, in full_dispatch
_request
 rv = self.handle_user_exception(e)
 File "C:\Python34\lib\site-packages\flask\app.py", line 1381, in handle_user_e
xception
 reraise(exc_type, exc_value, tb)
 File "C:\Python34\lib\site-packages\flask\_compat.py", line 33, in reraise
 raise value
 File "C:\Python34\lib\site-packages\flask\app.py", line 1475, in full_dispatch
_request
 rv = self.dispatch_request()
 File "C:\Python34\lib\site-packages\flask\app.py", line 1461, in dispatch_requ
est
 return self.view_functions[rule.endpoint](**req.view_args)
 File "C:\Users\nevernight\Desktop\visa + py\getPhone.py", line 6, in get_phone
 if request.method == 'POST':
NameError: name 'request' is not defined
127.0.0.1 - - [04/Feb/2015 22:35:32] "GET /get_phone/?__debugger__=yes&cmd=resou
rce&f=style.css HTTP/1.1" 200 -
127.0.0.1 - - [04/Feb/2015 22:35:32] "GET /get_phone/?__debugger__=yes&cmd=resou
rce&f=jquery.js HTTP/1.1" 200 -
127.0.0.1 - - [04/Feb/2015 22:35:33] "GET /get_phone/?__debugger__=yes&cmd=resou
rce&f=debugger.js HTTP/1.1" 200 -
127.0.0.1 - - [04/Feb/2015 22:35:34] "GET /get_phone/?__debugger__=yes&cmd=resou
rce&f=ubuntu.ttf HTTP/1.1" 200 -
127.0.0.1 - - [04/Feb/2015 22:35:34] "GET /get_phone/?__debugger__=yes&cmd=resou
rce&f=console.png HTTP/1.1" 200 -
127.0.0.1 - - [04/Feb/2015 22:35:34] "GET /get_phone/?__debugger__=yes&cmd=resou
rce&f=source.png HTTP/1.1" 200 -
127.0.0.1 - - [04/Feb/2015 22:35:35] "GET /get_phone/?__debugger__=yes&cmd=resou
rce&f=console.png HTTP/1.1" 200 -
shuttle87
16.1k12 gold badges80 silver badges107 bronze badges
asked Feb 4, 2015 at 17:29
3
  • 1
    Replace app.run() with app.run(debug=True) to get more information about what is going wrong. Though it seems the issue is that you haven't imported request. Commented Feb 4, 2015 at 17:32
  • 2
    to clarify your likely issue that @Sevanteri referenced from flask import Flask,request Commented Feb 4, 2015 at 17:36
  • thanks it works=) import request i mean, can any of you write it as an answer so I can accept it?) Commented Feb 4, 2015 at 17:41

2 Answers 2

2

The issue is that you haven't imported request. Like this:

from flask import Flask, request
answered Feb 4, 2015 at 17:49
Sign up to request clarification or add additional context in comments.

Comments

0

All variables that you put in a route you have to make sure that they are imported at the top of your flask route page.

answered Mar 26, 2019 at 17:16

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.