0

Hello I have a problem with a select/option html where you can select the page number, so page 1 of 100, 2 of 200 etc... and then goes to page 1, 2, 3 ...

Everything works in the following code apart from the fact that inside the select button, after clicking on the page you want to go, after refreshing the page, it goes back to page 1, it always shows: page 1 of 100, even if actually in the url the page number is correct. I should use a session variable, but the problem is that I'm struggling to understand how to use it with Flask/Jinja2 directly in the html page. Someone can help?

using {% set session['page'] = page %} and then {{ session['page'] }} it gives me syntax error:

TemplateSyntaxError: expected token 'end of statement block', got '['

{% set session['page'] = page %}
    <select onchange="window.location.href=this.value">

        {%- for page in range (1,(form.total_PAGE.data | int) + 1 ) %}

            <option value="{{ page }}">{{ session['page'] }}/{{ form.total_PAGE.data }}</option>
        {%- endfor %}
    </select>
Daniel Roseman
  • 588,541
  • 66
  • 880
  • 895
mar
  • 1
  • 4
  • You can't set a session variable in a template, that doesn't make sense. You do it in the view. – Daniel Roseman May 03 '19 at 08:32
  • found the solution, session variable doesn't enter in this matter, it's an html issue I must put selected='selected' in the option, and i have to change the variable name in the for loop, so like: {%- for each_page in range ... {% if each_page == page %} then – mar May 03 '19 at 10:13

2 Answers2

1

In general, you cannot assign to an object atribute using set, neither using obj['attr'] or obj.attr syntax (see the documentation). However, you can enable the expression-statement extension and then set the attribute using dict update like this:

{% do session.update({'page': page}) %}

But as already stated in the comment, this doesn't really make sense and can be solved more cleanly using other approaches.

Tomáš Linhart
  • 9,832
  • 1
  • 27
  • 39
  • could be cleaner right, considering that for you is obvious where you would put this code it's not for me cause I'm new to programming, so that is the code, but where must be put in the code? – mar May 03 '19 at 09:04
  • consider that it's a for loop, and the session variable is not iterable, i cannot put {%- for session['page'] it doesn't accept the ' [ ' – mar May 03 '19 at 09:04
  • found the solution, session variable doesn't enter in this matter, it's an html issue I must put selected='selected' in the option, and i have to change the variable name in the for loop, so like: {%- for each_page in range ... {% if each_page == page %} then – mar May 03 '19 at 10:15
0

I have tested what mentioned by Tomáš Linhart but not worked for me as I did not update Expression statement extension and modify default what worked to assign a session variable in jinja2 flask is:

  {% if session.update({session_var_name: []}) %}{% endif %}
Mahmoud Magdy
  • 662
  • 10
  • 13