from django.shortcuts import render, redirect
from django.urls import reverse
from django.utils.http import is_safe_url
from django.urls import reverse, NoReverseMatch


def get_next(request):
    # This is not a view
    return request.GET.get("next", request.META.get("HTTP_REFERER", "/"))


def render_code(code, request, context={}):
    return render(request, "%d.html" % code, context,
            status=code)


def render_400(request):
    return render_code(400, request)


def render_405(request):
    return render_code(405, request)


def render_403(request, message=''):
    return render_code(403, request,
            context={"message": message})


def get_client_ip(request):
    x_forwarded_for = request.META.get('HTTP_X_FORWARDED_FOR')
    if x_forwarded_for:
        ip = x_forwarded_for.split(',')[-1].strip()
    else:
        ip = request.META.get('REMOTE_ADDR')
    return ip


def get_redirect_url(request, default='/', next=None, allowed_hosts=None):
    if next is None:
        next = request.POST.get('next',
            request.GET.get('next', request.session.get('next')))
        if 'next' in request.session:
            del request.session['next']
    if next is None:
        keyword = request.GET.get('pk_keyword')
        if keyword and keyword.startswith('/'):
            next = keyword
    if not is_safe_url(url=next, allowed_hosts=allowed_hosts):
        next = None
    if next is None and default is not None:
        if not default.startswith('/'):
            default = reverse(default)
        next = default
    if next is None or not is_safe_url(url=next, allowed_hosts=allowed_hosts):
        next = request.META.get('HTTP_REFERER')
    if next is None or not is_safe_url(url=next, allowed_hosts=allowed_hosts):
        next = '/'
    return next


def get_redirect(request, **kwargs):
    url = get_redirect_url(request, **kwargs)
    try:
        return redirect(url)
    except NoReverseMatch:
        return redirect('/')

@@ -1,20 +1,162 @@
+try:%0A    from urllib.parse import parse_qs, urlsplit, urlunsplit%0Aexcept ImportError:%0A    from urlparse import parse_qs, urlsplit, urlunsplit%0A%0A
 from django.shortcut
@@ -212,16 +212,32 @@
  reverse
+, NoReverseMatch
 %0Afrom dj
@@ -274,56 +274,19 @@
 _url
-%0Afrom django.urls import reverse, NoReverseMatch
+, urlencode
 %0A%0A%0Ad
@@ -2091,8 +2091,545 @@
 ct('/')%0A
+%0A%0Adef update_query_params(url, params):%0A    %22%22%22%0A    Given a URL, update the query parameters and return the%0A    modified URL.%0A%0A    %3E%3E%3E update_query_params('http://example.com?foo=bar&biz=baz', %7B'foo': 'stuff'%7D)%0A    'http://example.com?foo=stuff&biz=baz'%0A%0A    %22%22%22%0A    scheme, netloc, path, query_string, fragment = urlsplit(url)%0A    query_params = parse_qs(query_string)%0A    query_params.update(params)%0A%0A    new_query_string = urlencode(query_params, doseq=True)%0A%0A    return urlunsplit((scheme, netloc, path, new_query_string, fragment))%0A

Add update query params helper function