0

I have my python code where I use html file. Please see below code:

@app.route('/',endpoint='buf')
def index():
    page = """
    <DOCTYPE! html>
    <html lang="en-US">
    <head>
    <title>Hello World Page</title>
    <meta charset=utf-8">
    </head>
    <body>
    <form action="/hello" method="GET">
    First: <input type="text" name="fname"><br>
    Last name: <input type="text" name="lname"><br>
    <input type="submit" value="Submit">
    </form>
    </body>
    </html>
    """   
    return page


@app.route('/hello',endpoint="new",methods=['GET'])
def login():
         return 'hello %s ' % (request.form['fname'])

This gives me an html page , which has a form with two fields and a submit button. I use virtual env to run this.

Now I want to get the fields first name and last name in my program so that when user click submit I want to display Hello "first name""last name"

Purpose :- to learn how to get the html element values when provided with a code In a python file.

user3369157
  • 137
  • 1
  • 9
  • 1
    Easy... You read everything regarding `POST` here http://flask.pocoo.org/docs/quickstart/ – Torxed Mar 25 '14 at 16:30

1 Answers1

2

Several things to do to make it work:

  • add POST and GET to supported methods of an app.route:

    @app.route('/', methods=['POST', 'GET'])
    
  • in the view, handle both GET (when the user opens up a web page) and POST (when the user submits the form)

  • extract html code into an html template file and render it in the view (docs on rendering templates)

Here's the code with changes:

from flask import render_template
...

@app.route('/', methods=['POST', 'GET'])
def index():
    if request.method == 'POST':
        # use request.form['fname'] and request.form['lname'] 
        # to access form input values
    else:
        return render_template('index.html')

Also see documentation page.

Hope that helps.

alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195
  • and where should my code fit in here? – user3369157 Mar 25 '14 at 16:38
  • @user3369157 in a separate html file under `templates` directory ([docs](http://flask.pocoo.org/docs/quickstart/#rendering-templates)). – alecxe Mar 25 '14 at 16:51
  • That what I said, I don't want to use html file. I just wrote my html code in the index() function and I am returning the page. Is there a way to get the input fields from the code I posted? – user3369157 Mar 25 '14 at 17:01
  • @user3369157 it is not actually relevant where are you putting that html form code. The answer still briefly explains how you can get the form input values inside the `index` view. – alecxe Mar 25 '14 at 17:07
  • Hello , Please see my edited code. I added the methods and tried to use it but I get HTTP 404 error. When I hit submit I see this url - http://0.0.0.0:5001/POST?fname=hello&lname= . Did i write the code correctly? – user3369157 Mar 25 '14 at 19:15
  • @user3369157 sure, try adding `action="" method="post"` attributes to the `form` tag. – alecxe Mar 25 '14 at 19:16
  • Thanks, I added them , But now my page does not get redirected to any othe page, when I hit submit the page just refreshes. How can make it to print Hello "fname" "lname" – user3369157 Mar 25 '14 at 21:15
  • 1
    @user3369157 on the line `print request.form['fname']` you should `return 'Hello %s %s' % (request.form['fname'], request.form['lname'])`. – alecxe Mar 25 '14 at 21:18
  • Edited my code as above. But I am not sure why I get the same page again when I hit Submit. IS the route correct? – user3369157 Mar 25 '14 at 21:23
  • Your form is using `method="GET"` and @alecxe is showing you how to make it work for `method="POST"`. You can either change the method in your HTML to "POST" or you can rewrite your Python code to handle both cases under the "GET" method. In any case, I think you need to read up on HTTP before you get much further along. This stuff shouldn't be as mystical as you're making it. – Joe Holloway Mar 25 '14 at 21:56
  • I edited my code , but it does not get me the result. I get an error saying Bad request type. When I use requests.form it gives this error however if I jus say return "hello" it gives me hello. I think the requests object can not get the form elements. There is some thing wrong in the way I access the form element. Any help is appreciated. – user3369157 Mar 25 '14 at 22:28
  • when I try to access request.form['fname'] I get error as 'module' object has no attribute 'form' – user3369157 Mar 25 '14 at 22:35
  • @user3369157 check your import statement - it should be `from flask import request`. – alecxe Mar 25 '14 at 22:37
  • When I try using this way I get - Bad Request ,The browser (or proxy) sent a request that this server could not understand. – user3369157 Mar 25 '14 at 22:43
  • Got it !! I have to use POST instead of GET. But is it not possible to use request object for GET? – user3369157 Mar 25 '14 at 23:09
  • @user3369157 it's possible, you can do the same with get. Set `method="GET"` on the form and see what comes inside `request.args`. See also: http://stackoverflow.com/questions/13279399/how-to-obtain-values-of-request-variables-using-python-and-flask – alecxe Mar 26 '14 at 00:22