Reputation: 1308
i have a sqlalchemy query which renders a template with a couple of settings.
below you can find very simplified code to give an idea of what is going on. This code puts a checkbox field for a setting on every page, and there is no fixed nr of settings at the moment, it depends on the size of the table. As far as the pagination goes, this works fine. I can go to next and previous page.
The submit button on the page only posts the checkbox value of the last page. Is it possible to also remember and/or save the input from all pages, not just the last page?
@app.route('/settings')
def settings():
page = request.args.get('page', 1, type=int)
settings = Settings.query.paginate(page, 1, False)
next_url = url_for('settings', page=settings.next_num) \
if settings.has_next else None
prev_url = url_for('settings', page=settings.prev_num) \
if settings.has_prev else None
inputtype = 'checkbox'
return render_template("settings.html",
settings = settings,
inputtype = inputtype,
next_url = next_url,
prev_url = prev_url
)
template would be something like this.
<div class="form-check">
{% for setting in settings %}
<input type="{{ inputtype }}" value="{{ setting }}" {{ setting }}
{% endfor %}
<div class=pagination>
{% if prev_url %}
<a href=" {{ prev_url }} "> Previous </a>
(% endif %}
{% if next_url %}
<a href=" {{ next_url }} "> Next </a>
{% endif %}
</div>
<div class="panel-footer">
<input class="btn btn-primary" role="button" type="submit" value="Submit">
</div>
Upvotes: 1
Views: 1175
Reputation: 1308
I fixed this without using javascript. I came across this answer and seems to do the trick. It simply does a post request and jumps to the next page. @hugo, thanks for your answers, it certainly helped me looking in the right direction.
Cannot Generate a POST Request on Flask using url_for
Upvotes: 0
Reputation: 159
I get the feeling that if you submit you only submit the settings on the current page. Only the current settings are on the page and it would not make much sense to add all of them to the page.
I think that what you want is not possible on multiple pages if you use links to got to the previous and next settings. If you make a change on page 1 and then click next the changes made on page 1 are not saved anywhere so they are lost.
Maybe it is possible to make previous and next also post to settings. This way you get the settings from that page and can make a temporary settings object that you can process when you click commit.
Upvotes: 1