0

I tried this approach to pass the form input variable to function via url_for method. But its not working. can anyone check and let me know whats wrong in this code.

Function:

@app.route('/report_data/<year>/<week>', methods = ['POST'])
def report_data(year,week):
  print year
  print woy

HTML Code :

 <html>
<body>
   <form action="{{  url_for('report_data', year=n_year, week=n_woy) }}" method="post">
        <h3> Enter the details  </h3>
        Year :
        <input type="text" name="n_year"> <br>
        <br>
        Week :
        <input type="text" name="n_woy"> <br>
        <br>
        <input type="submit" value="Submit"> <br>
        </form>
        </body>
        </html>

Issue: Getting "None" for both variable.

1 Answer 1

1

Firstly, How do you provide year, week values before submitting them in your HTML code ? Did you render the HTML template in your function? If not, how does your flask function knows that you were seeking from that specific form?

If this is your Flask app code -

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

@app.route('/report_data', methods = ['POST', 'GET'])
def report_data():
    if request.method == 'POST':
        result = request.form
        query_year = result['n_year'].encode('ascii', 'ignore')
        query_week = result['n_woy'].encode('ascii', 'ignore')
        print(query_year, query_week)
    return render_template('so_test.html')

if __name__ == '__main__':
   app.run(debug=True)

And opened the URL -

http://127.0.0.1:5000/report_data

And I get this picture. enter image description here

Once I post the data, I can see this data in my Flask app console.

('2018','08')

Sign up to request clarification or add additional context in comments.

Comments

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.