Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

195
Views
Custom template tag don't work as expected (django)

My custom template tag is called change_page and should change page=x in the url without changing anything else in the url. (That means anything in the url, before and behind the ?.

That's the template code part: (it works with the standard django paginator)

 {% if page_obj.has_previous %}
        <a href="{{ request.path }}?{% change_page current=request.GET.urlencode page=page_obj.previous_page_number %}" class="left_sharp">Zurück</a>
     {% else %}
        <a tabindex="2" href="" class="noteditable">Zurück</a>
     {% endif %}

     <div class="screen_only">
     {% for i in paginator.page_range %}
      {% ifequal page_obj.number i %}
        <a href="" tabindex="2" class="noteditable">{{ i }}</a>
      {% else %}
        <a href="{{ request.path }}?{% change_page current=request.GET.urlencode page=i %}">{{ i }}</a>
      {% endifequal %}
     {% endfor %}
     </div>

     {% if page_obj.has_next %}
        <a href="{{ request.path }}?{% change_page current=request.GET.urlencode page=page_obj.next_page_number %}" class="right_sharp">Weiter</a>
     {% else %}
        <a href="" tabindex="2" class="noteditable">Weiter</a>
     {% endif %}
    </nav>
    {% endif %}

The code of the template tag change_page:

@register.simple_tag
def change_page(current="", page=""):
    args = current.split('&')       
    all=''

    for arg in args:
        all += re.sub(r'page=[0-9]+', 'page='+str(page), arg) + '&'

    all = all[:-1]

    return all

It doesn't work. It allways returns an empty string. What did I wrong?

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

It didn't worked because I did nothing when there was no page argument in request.GET.urlencode.

I added this code in the beginning of the method, and now it works fine unless it adds a = to arguments which are only keys, and have no values.

if current == "":
        return 'page='+str(page)

    if not re.compile('page=[0-9]+').match(current):
        return current + '&page=' + str(page)
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!