1

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>
gittert
  • 1,238
  • 2
  • 7
  • 15

2 Answers2

1

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.

Hugo
  • 159
  • 2
  • 11
  • Yep thats what I'm afraid of and in fact that is how it it working. Kind of logical way of working if you think about it. Any ideas on how to get a next button to also do a post request? I cannot find anything on this subject online. – gittert Mar 17 '18 at 17:59
  • (https://stackoverflow.com/questions/8169027/how-can-i-submit-a-post-form-using-the-a-href-tag) – Hugo Mar 17 '18 at 18:09
  • If I understand correctly, this would only be possible with javascript, no python way of doing this? – gittert Mar 17 '18 at 19:55
  • There is no way to do this in python as the browser needs to make a HTTP POST to your flask application, so the page needs to do a HTTP POST not only on submit, but also on previous and next page. – Hugo Mar 17 '18 at 20:11
0

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

gittert
  • 1,238
  • 2
  • 7
  • 15
  • I am still a bit confused I have the same problem now, I have 36 or so pages, with radio buttons on it, but only the last page where something is selected gets posted. – mp252 Jul 24 '18 at 07:18
  • Please post some code so we can try to be of anny help – gittert Jul 25 '18 at 23:19
  • I managed to do it with jquery ajax, it doesn't save all in one go, but when you move from one page to another it saves then – mp252 Jul 26 '18 at 08:52
  • I solved it like this, you might have to change it a bit to fit within your app. if request.method == "POST": if request.form["submit"] == "next": do_something() if quiz.has_next: next_url = url_for('foo’, **kwargs) the template contains the following line: – gittert Jul 26 '18 at 11:07