Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

205
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda