Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

196
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda