user2950593
user2950593

Reputation: 9627

Simple form submit python

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 -

Upvotes: 0

Views: 1310

Answers (2)

Ethan KoehlerBryant
Ethan KoehlerBryant

Reputation: 19

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.

Upvotes: 0

Sevanteri
Sevanteri

Reputation: 4038

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

from flask import Flask, request

Upvotes: 2

Related Questions