In my program, I have a GET request sent from a form, to a Flask View.
This is the form:
<form method="GET" action="{{url_for('rerouter')}}">
{{ form.hidden_tag() }}
<fieldset class="form-group">
<legend class="border-bottom mb-4">Search</legend>
<div>
{{ form.welder_selector.label(class="form-control-label") }}
{{ form.welder_selector(class="searchable-welder-js",style="width: 28%") }}
{{ form.wps_selector.label(class="form-control-label") }}
{{ form.wps_selector(class="searchable-wps-js",style="width: 27.8%") }}
{{ form.process_selector.label(class="form-control-label") }}
{{ form.process_selector(class="searchable-process-js",style="width: 27.8%") }}
</div>
</fieldset>
<div class="form-group">
{{ form.submit(class='btn btn-outline-success') }}
</div>
</form>
The form method I have set as GET and action also is sent to the view:
The corresponding view:
@app.route("/rerouter", methods=['GET', 'POST'])
def rerouter():
if request.method == 'GET':
value1 = request.args.get('welder_selector')
value2 = request.args.get('wps_selector')
value3 = request.args.get('process_selector')
print("Im at rerouter with : ", value1, value2, value3)
if value1 is not 0 or None:
if value2 is 0 or None:
if value3 is 0 or None:
return redirect(url_for('welderdatabase', welder_id=value1))
else:
return redirect(url_for('home'))
....
The HTML form is made here in this view :
@app.route("/", methods=['GET', 'POST'])
@app.route("/home", methods=['GET', 'POST'])
def home():
form = TrialSearchMultipleForm()
return render_template('home.html', title='Home', form=form)
I have successfully implemented the program using POST method, but I have later learnt that the right way is to use GET for sending form data.
What I caught in the console
127.0.0.1 - - [29/Apr/2019 09:10:20] "GET /rerouter?welder_selector=__None&wps_selector=__None&process_selector=__None&submit=Search HTTP/1.1" 302
Im at rerouter with : __None __None __None
So exactly what I'm looking for is :
How do I solve this ?
Expected Result :
Im at rerouter with : None None None or Im at rerouter with : 1 None None (based on the input)
And successful redirection to the page using the conditions. My code is not that clean because this section of the program was constantly changing and I needed to keep everything short, so please bear with me.
I'll be extremely grateful for ANY pointers or suggestions or helpful tips.
Don't know if it will solve your problem - just taking a cursory glance at your python script.
if value1 is not 0 or None:
This isn't doing what you think it's doing. 'None' is considered falsey. What you've written is equivalent to the following two independent boolean expressions:
if (value1 is not 0) or (False):
Therefore, the only part of that expression that has any bearing on whether the body of the if-statement is executed is the first part (value1 is not 0). This means that your if-statement's body will not be executed if value1 is None, should that ever be the case.
In addition, assuming that's fixed, you probably don't want to separate the two conditions with an 'or'. It seems to me that value1 must not be either of those values, in which case you would use 'and'.
Incidentally, this may not be so critical, but I would check whether value1 is not equal to the value zero, instead of checking whether their identities are not the same.
According to pep8, however, when one is checking if something is None, one should prefer (value1 is not None) over (value1 != None). The fixed condition would look like this, in my eyes:
if value1 != 0 and value1 is not None: