Python Flask render_template returns HTML script instead of redirecting to the HTML page -
i have python script uses flask web framework let users ask question , depending on questions, application should ask questions user on second webpage. answers questions evaluated based on questions , displayed on initial webpage.
model.py
### importing flask ### flask import flask, render_template, request, session, redirect, url_for ### initializing flask ### app = flask(__name__) @app.route('/') def index(): return render_template('init.html') @app.route('/handle_data', methods = ['post', 'get']) def handle_data(): ### user inputs question ### userquestion = request.form['userquestion'] def model(): message = "depends on user" if message == "depends on user": return render_template('user_information.html', userquestion = userquestion) else: message = "your answer abc." return message message = model() return render_template('init.html', userquestion = userquestion, message = message) @app.route('/user_information', methods = ['post', 'get']) def user_information(): userlevel = request.form['userlevel'] userdoj = request.form['userdoj'] usertype = request.form['usertype'] message = "answer eligibility." return render_template('init.html', userlevel = userlevel, userdoj = userdoj, usertype = usertype, message = message) if __name__ == '__main__': app.run()
these 2 html files:
init.html (initial webpage)
<!doctype html> <html> <head> <title>human resources</title> <!-- for-mobile-apps --> </head> <body> <div class="main"> <div class="w3_agile_main_grid"> <h2>human resource portal</h2> <br> <p>hi</p> <form action="{{ url_for('handle_data') }}" method="post" class="agile_form"> <input type="text" name="userquestion" placeholder="ask question..." required=""> <input type="submit" value="submit"> </form> <p>{{ message }}</p> </div> </div> </body> </html>
user_information.html (second webpage)
<!doctype html> <html> <head> <title>human resources</title> </head> <body> <div class="main"> <div class="w3_agile_main_grid"> <h2>human resource portal</h2> <form action="{{ url_for('user_information') }}" method="post" class="agile_form"> <!--<input type="text" name="username" placeholder="enter name." required="">--> <input type="text" name="userlevel" placeholder="what level?" required=""> <input type="text" name="userdoj" placeholder="what date of joining?" required=""> <input type="text" name="usertype" placeholder="are on sabbatical or specialist?" required=""> <input type="submit" value="submit"> </form> </div> </div> </body> </html>
when execute script , enters question, html code user_information.html answer not want.
ouput after click submit: https://ibb.co/cwhrpk
expected output after click submit: https://ibb.co/c7cfh5
i can desired output if remove model() construct make code inefficient because in actual application have call model() multiple times different parameters.
can please suggest me approach should take? i'm totally stuck in part. thanks, appreciated!
your nested model()
function not make sense @ all. returns result of render_template
, complete response including http headers etc. if try , insert template, jinja forced try , convert string, gives result see.
this not @ way compose templates. jinja supports template inheritance; should call render_template
once only, using child template inherits common base.
Comments
Post a Comment