mirror of
				https://github.com/encode/django-rest-framework.git
				synced 2025-11-04 09:57:55 +03:00 
			
		
		
		
	Thanks to Jon Dufresne (@jdufresne) for review. Co-authored-by: Asif Saif Uddin <auvipy@gmail.com> Co-authored-by: Rizwan Mansuri <Rizwan@webbyfox.com>
		
			
				
	
	
		
			28 lines
		
	
	
		
			1.0 KiB
		
	
	
	
		
			Python
		
	
	
	
	
	
			
		
		
	
	
			28 lines
		
	
	
		
			1.0 KiB
		
	
	
	
		
			Python
		
	
	
	
	
	
from urllib import parse
 | 
						|
 | 
						|
from django.utils.encoding import force_str
 | 
						|
 | 
						|
 | 
						|
def replace_query_param(url, key, val):
 | 
						|
    """
 | 
						|
    Given a URL and a key/val pair, set or replace an item in the query
 | 
						|
    parameters of the URL, and return the new URL.
 | 
						|
    """
 | 
						|
    (scheme, netloc, path, query, fragment) = parse.urlsplit(force_str(url))
 | 
						|
    query_dict = parse.parse_qs(query, keep_blank_values=True)
 | 
						|
    query_dict[force_str(key)] = [force_str(val)]
 | 
						|
    query = parse.urlencode(sorted(list(query_dict.items())), doseq=True)
 | 
						|
    return parse.urlunsplit((scheme, netloc, path, query, fragment))
 | 
						|
 | 
						|
 | 
						|
def remove_query_param(url, key):
 | 
						|
    """
 | 
						|
    Given a URL and a key/val pair, remove an item in the query
 | 
						|
    parameters of the URL, and return the new URL.
 | 
						|
    """
 | 
						|
    (scheme, netloc, path, query, fragment) = parse.urlsplit(force_str(url))
 | 
						|
    query_dict = parse.parse_qs(query, keep_blank_values=True)
 | 
						|
    query_dict.pop(key, None)
 | 
						|
    query = parse.urlencode(sorted(list(query_dict.items())), doseq=True)
 | 
						|
    return parse.urlunsplit((scheme, netloc, path, query, fragment))
 |