I have this code: (template)
// Send the changed input data to the server
(function() {
window.addEventListener('DOMContentLoaded', () => {
document.querySelectorAll('input').forEach(elem => {
elem.addEventListener('change', function() {
if(this.name.substring(0,3) != "add")
{
const formData = new FormData();
formData.append(this.name, this.value);
fetch('/settings', {
method: 'post',
body: formData
});
}
});
});
});
})();
Here needs showing message in template (app.py)
if len(value) > 30:
flash("Error! Header title max length is 30 characters", "error")
return redirect("/settings")
layout.html has:
<div class="alert alert-primary mb-0 text-center" role="alert">
{{ get_flashed_messages() | join(" ") }}
</div>
As redirect() not working in case of form submission with AJAX, How can show appropriate message about submitted data?
Or how can send back the conditional result from app.py to template with AJAX and show message in a <div>?
Flash message will appear only when you reload the page as they are rendered on the server. Redrawing the body with the response should solve the problem. try the following:
Probably you need to redraw the whole page with the response.