0

Looking at this, it should work right?

It seems like the get request is overwriting the post request return because it only renders no error.

Why is that?


index.html

{% if error %}
    <p>{{ error }}</p>
{% else %}
    <p>no error</p>
{% endif %}

main.py

@app.route('/', methods=['GET', 'POST'])
def index():
    if request.method == 'POST':
        post_data = request.get_json(force=True)
        if post_data['message'] == False:
            return render_template('index.html', error='not detected')
    return render_template('index.html')

edit: still haven't found out what's wrong.

hwhat
  • 326
  • 1
  • 5
  • 14

2 Answers2

0

Look closely at post_data['message']. Are you absolutely sure it's False? If it's the string 'False', the code falls through and won't render an error.

Dave W. Smith
  • 24,318
  • 4
  • 40
  • 46
  • ya i'm posting: ```body: JSON.stringify({'message': false})```. tried it with False as well, which gives ```Uncaught ReferenceError: False is not defined``` – hwhat Nov 27 '22 at 06:41
0

Try using else or elif.

else:

@app.route('/', methods=['GET', 'POST'])
def index():
    if request.method == 'POST':
        post_data = request.get_json(force=True)
        if post_data['message'] == False:
            return render_template('index.html', error='not detected')
    else:
        return render_template('index.html')

elif (recommended):

@app.route('/', methods=['GET', 'POST'])
def index():
    if request.method == 'POST':
        post_data = request.get_json(force=True)
        if post_data['message'] == False:
            return render_template('index.html', error='not detected')
    elif request.method == 'GET':
        return render_template('index.html')