0

I want to do a return with the ans variable only when i get the "question" variable from the HTML page , Otherwise, do it without the variable. But even though that the code enter to the if statment (when i'm print the 'question' or the 'ans'), the second return is still being made (the one sent without the extra variable) Is there another way to render each time with a different variable? What am I doing wrong?

app = Flask(__name__)

@app.route("/")
def home():
    if 'question' in request.args:
        question= request.args.get('question')
        print(question)
        print(respond(question))
        return render_template("HTML.html" , ans=respond(question))
    else:
        return render_template("HTML.html", ans =respond(""))

if __name__ == '__main__':
    port = int(os.environ.get('PORT', 5000))
    if port == 5000:
        app.debug = True

    app.run(host='0.0.0.0', port=port)

2 Answers2

0

I would recommend making a POST request from the client since its not recommended practice to send data in the body of a GET request. So you could try the following:

Can you try:

$.ajax({
        url:"/",
        type:"POST",
        data: { question: "question_text" },
        success: function(response) {
            //Do Something
        },
        error: function(xhr) {
            //Do Something to handle error
        }               
    });

Then change your Flask to accept a POST request:

@app.route("/", methods=['POST'])
def home():
    json = request.json
    if 'question' in json:
        question = json['question']
        print(question)
        print(respond(question))
        return render_template("HTML.html" , ans=respond(question))
    else:
        return render_template("HTML.html", ans =respond(""))
jignatius
  • 6,304
  • 2
  • 15
  • 30
  • Thank you for your help! But No the parameter actually send good, I can see them in the print.. but after the update print It was returned the second call without the ans parameter and that the problem – Linperetz Apr 05 '20 at 21:22
  • @Linperetz Did you add url method in the app route in Flask: `methods=['POST']`? If you didn't then the server will only accept a GET request. – jignatius Apr 06 '20 at 11:06
-1

I am guessing that you get the input of question by a post request? If so you could do:

if request.method == 'POST' and 'question' in request.arg.get('question'):
    ## Do something
    return  return render_template("HTML.html" , ans=respond(question))
else:
    ## do something else
    return  return render_template("HTML.html")
  • No I get the input from GET, through this code $.getJSON( '/', $.param({question : question }, true) ); , and i try waht you said before and it did not solve it.. you think i should switch the method to post? – Linperetz Apr 05 '20 at 18:06
  • That's some weird code. Why not make two separate endpoints, one for GET and one for POST instead of doing a `if request.method ==`? Could it be that you hate writing clean code? – Zun Apr 06 '20 at 09:48
  • Separation of concern (https://en.wikipedia.org/wiki/Separation_of_concerns) : you want one method that responsible for handling POST and one for GET. This makes maintainability and debugging easier as well. This also makes it easier to add DELETE/HEAD functions in the future – Zun Apr 07 '20 at 11:29
  • the html page make a reload when i make 2 separate endpoints, one for GET and one for POST, and show me the first html Without the changes. `app = Flask(__name__) @app.route("/", methods=['POST']) def home(): question = request.form['message-to-send'] print(question) print(respond(question)) return render_template("HTML.html" , ans=respond(question)) @app.route("/", methods=['GET']) def home2(): return render_template("HTML.html")` **how can i block this reload??** – Linperetz Apr 07 '20 at 12:34