S
S
sortfact3332020-12-09 11:13:58
Django
sortfact333, 2020-12-09 11:13:58

How do I change a variable inside tempalte?

I have two variables and want to add and subtract inside tempalte.
I can't do it inside views.py since it's pagination.

!--Pagination-->
    <nav aria-label="Page navigation example">
        <ul class="pagination justify-content-center">
        {% if movies.has_previous %}
            <li class="page-item">
            <a class="page-link" href="?page={{ movies.previous_page_number }}">Previous</a>
          </li>
        {% else %}
            <li class="page-item disabled">
            <a class="page-link" href="#" tabindex="-1" aria-disabled="true">Previous</a>
          </li>
        {% endif %}

        {% if movies.number|add:'-4' > 1 %}
            <li class="page-item"><a class="page-link" href="?page={{ movies.number|add:'-5' }}">&hellip;</a></li>
        {% endif %}

        {% for i in movies.paginator.page_range %}
            {% if movies.number == i %}
                <li class="page-item active" aria-current="page">
              <span class="page-link">
                {{ i }}
                <span class="sr-only">(current)</span>
              </span>
            </li>
            {% elif i > movies.number|add:'-5' and i < movies.number|add:'5' %}
                 <li class="page-item"><a class="page-link" href="?page={{ i }}">{{ i }}</a></li>
            {% endif %}
        {% endfor %}

        {% if movies.paginator.num_pages > movies.number|add:'4' %}
           <li class="page-item"><a class="page-link" href="?page={{ movies.number|add:'5' }}">&hellip;</a></li>
        {% endif %}

        {% if movies.has_next %}
            <li class="page-item">
            <a class="page-link" href="?page={{ movies.next_page_number }}">Next</a>
          </li>
        {% else %}
            <li class="page-item disabled">
            <a class="page-link" href="#" tabindex="-1" aria-disabled="true">Next</a>
          </li>
        {% endif %}
      </ul>
    </nav>

In other words, I want to cut
{{ i|add:'5'}}
  {{ i|add:'-5'}}
  {{ i|add:'4'}}
  {{ i|add:'-4'}}

Before
X == 5
  {{ i|add:'{{x}}'}}
  {{ i|add:'-{{x}}'}}
  {{ i|add:'{{x}}-1'}}
  {{ i|add:'-{{x}}-1'}}

To be able to conveniently change only one variable

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
mkone112, 2020-12-09
@mkone112

Firstly, there is a with tag, and secondly, I'm not sure that dragging logic into a template is a good idea.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question