Stephen
Stephen

Reputation: 6319

Remove value from GET dictionary and redirect to new URL

I want to have a view in my app that allows the user to remove one of many filters from the GET list, then redirect using the remaining variables in the list. How can I achieve this? Possibly having one filter-remove view that works for all the variables the user can set in the filter.

Upvotes: 4

Views: 3109

Answers (2)

Spyros
Spyros

Reputation: 540

I think you can achieve this using jquery's library query plugin

<script>
    function deleteFilterFromQuery(filter){
        //remove this filter from url
        var newquery = $.query.Remove(filter);
        //redirect to the new url
        window.location = newquery;
    }
</script>

<a onclick="deleteFilterFromQuery(this.rel)" id="filter1RemoveLink" rel="filter1">remove filter 1 </a>
<a onclick="deleteFilterFromQuery(this.rel)" id="filter1RemoveLink" rel="filter2">remove filter 2</a>
<a onclick="deleteFilterFromQuery(this.rel)" id="filter1RemoveLink" rel="filter3">remove filter 3</a>

Upvotes: 1

Bernhard Vallant
Bernhard Vallant

Reputation: 50796

If I understand you right you're looking for something like this:

from django.http import HttpResponseRedirect

def myview(request):
    mypath = ..... #your redirect
    remove_get_variable = request.GET.pop('myvar')
    return HttpResponseRedirect(mypath)

If you need this more often you could also include this functionality in a middleware!

Upvotes: 6

Related Questions