Using Flask. This must be a basic gotcha I just don't know about.
I have two templates, index.html
and index8.html
index()
sets url
to one of these two strings, based on a session parameter, and returns render_template(url, data={...})
Then I've got a toggleMode()
which sets the session parameter (from a POST), and then calls return index()
It does seem to run the index()
code... it prints out index.html
or index8.html
correctly, which is passed to render_template, and returned, and returned... but it never uses index8.html
.
If you change html template files, do you need a redirect? Is that it?
Why does Flask not change templates, in this situation? I'm printing the template name, and it is 'index8.html'
passed in to render_template
. But it still renders as though I had passed in 'index.html'
. (Or, rather, it doesn't do a new render, at all, despite Flask returning a 200 reply to toggleMode()
)
EDIT:
Ok minimal example...
@app.route('/')
def index():
try:
mode = session['MODE'] == "Four"
except:
session['MODE'] = "Four"
mode = True
print ('index.html' if mode else 'index8.html')
url4 = 'index.html'
url8 = 'index8.html'
return render_template(url4 if mode else url8, data={...})
@app.route('/toggleMode', methods=['POST'])
def toggle_mode():
data = request.get_json()
print (data)
session['MODE'] = data[0]['mode']
print(session['MODE'])
return index()
I get the correct index.html
or index8.html
printed out, but toggling the mode always renders with index.html
source https://stackoverflow.com/questions/72453184/flask-index-returns-render-template-why-does-return-index-not-work
Comments
Post a Comment