Here I am sharing an example code of what i am trying to achieve.
my_bluprint = Blueprint('index',
__name__,
template_folder=template_dir)
@my_bluprint.route('/print', methods=['GET', 'POST'])
def my_function_2():
print('i was redirected')
if request.method == 'POST':
age = request.form.get('age')
print('AGE: ', age)
return render_template("form2.html")
@my_bluprint.route('/test', methods=['GET', 'POST'])
def my_function():
if request.method == 'POST':
print('my_function')
# do something
name = request.form.get('name')
print('my_name', name)
if name == 'root':
print('name is ', name)
return redirect(url_for("index.my_function_2"))
# age = request.form.get('age')
print('AGE: ',age) # I need input age from my_function_2 here
age_trans = my_module.ageMul(age)
print('TransAGE: ', age_trans)
return render_template("result.html", result=age_trans)
return render_template("form1.html")
I need to render different templates depending on the input from previous template. On run time my program will use the input from one template to decide which template to redirect next. I tried rendering the templates the way i have used in my example code but I need inputs from previous templates for further processing combined with inputs of all the other templates which were rendered. EDIT
i am able to redirect to other routes in my code. but i need input from the template i am using in my_function_2() in my_function() after the execution of my_function_2(). which means i need to come back to main route i-e /test with data from my_function_2. I need help in understanding how can I do that
Another Idea
is there a way to render different templates using conditions in same function lets say my_function(). If input from one html form makes the condition true render template A else render template B ? meanwhile retaining the inputs from first template rendered in my_function()? in this way i can have only one route rendering multiple templates ? but i want to keep data from all the templates previously rendered
what would be the good approach of doing this ?
EIDT 2
i tried this
@my_bluprint.route('/test', methods=['GET', 'POST'])
def my_function():
if request.method == 'POST':
#name = request.form.get('name')
session['name'] = request.form['name']
print('my_name', session['name'])
if session['name'] == 'root':
print('name is ', session['name'])
#return redirect(url_for("index.my_function_2"))
return render_template("form2.html")
age = request.form.get('age')
print('AGE ',age)
age_trans = my_module.ageMul(age)
print('TransAGE: ', age_trans)
print(session['name'])
return render_template("result.html", result=age_trans)
return render_template("form1.html")
First run goes smooth till return render_template("form2.html") after the form submitted here control goes back to if request.method == 'POST': and execute it from the start and gives me error here session['name'] = request.form['name'] but i want to the program control to always move forward with the values it got in first run of render_template("form1.html") until the page(url) is refreshed
Aucun commentaire:
Enregistrer un commentaire