How to remove a GET parameter properly
<p>I needed to remove a parameter from the <code>GET</code> url. I didn't find a "proper" way to do it, so I wrote my own filter like this:</p>

<pre><code>@register.filter(name='url_without_parameter')
def url_without_parameter(arg1, arg2):
    """
    Removes an argument from the get URL.
    Use:
        {{ request|url_without_parameter:'page' }}

    Args:
        arg1: request
        arg2: parameter to remove
    """
    if arg1.GET.getlist(arg2):
        # get all parameters :
        full_url = urlparse.urlparse(arg1.get_full_path())
        parameters = {}
        # reconstruct query
        for key, value in urlparse.parse_qsl(full_url.query):
            if parameters.get(key, None) is None:
                parameters[key] = value
        # remove parameters
        if parameters.get(arg2) is not None:
            del parameters[arg2]
        arg1.META['QUERY_STRING'] = u'&amp;'.join([
            u'{}={}'.format(key, value)
            for key, value in parameters.iteritems()])

    return arg1.get_full_path()
</code></pre>

<p>Then I'm using it like this:</p>

<pre><code>{{ request|url_without_parameter:'page' }}
</code></pre>

<p>And the advanced way is:</p>

<pre><code>href="{{ request|url_without_parameter:'page' }}&amp;page={{ obj.page }}"
</code></pre>

<p>My problem is when there's not other parameters in the <code>GET</code> of the URL. This creates URLs like "<code>/test&amp;page=1</code>" which is not good.</p>

<p>So I had to write another filter to make this work like that:</p>

<pre><code>href="{{ request|url_without_parameter:'page' }}{{ request|url_get_separator }}page={{ obj.page }}"
</code></pre>

<p>With my <code>url_get_separator</code> filter.</p>

<pre><code>@register.filter(name='url_get_separator')
def url_get_separator(arg1):
    """
    Returns '?' or '&amp;' if there are QUERY parameters in the URL
    Use:
        {{ request|url_get_separator }}

    Args:
        arg1: request
    """
    return u'&amp;' if arg1.META.get('QUERY_STRING', '') else u'?'
</code></pre>

<p>I find all of this pretty ugly and hard to read and hard to maintain. Do you know any other better way to simply replace a value of a <code>GET</code> in the URLs?</p>