[Fixed]-How to remove a GET parameter properly

1๐Ÿ‘

โœ…

I use this tag mostly for pagination links within my project, i.e. to retain everything but switch the page number for the numerical hyperlinks, youโ€™ll notice I trash the _pjax param if it exists:

@simple_tag
def query_transform(request, **kwargs):
    """usages: {% query_transform request page=1 %}"""
    updated = request.GET.copy()

    for k, v in kwargs.iteritems():
        updated[k] = v

    # trash any pjax params, we never want to render those
    try:
        del updated['_pjax']
    except KeyError:
        pass

    return updated.urlencode()

I think maybe you want something similar, and maybe the trick you were missing was to call copy on the request.GET dictionary and mess with that, rather than the URL.

Leave a comment