def urlparams(url_, fragment=None, query_dict=None, **query):
 url_ = urlparse.urlparse(url_)
 fragment = (fragment if (fragment is not None) else url_.fragment)
 q = url_.query
 new_query_dict = (QueryDict(smart_str(q), mutable=True) if q else QueryDict('', mutable=True))
 if query_dict:
  for (k, l) in query_dict.lists():
   new_query_dict[k] = None
   for v in l:
    new_query_dict.appendlist(k, v)
 for (k, v) in query.items():
  if isinstance(v, list):
   new_query_dict.setlist(k, v)
  else:
   new_query_dict[k] = v
 query_string = urlencode([(k, v) for (k, l) in new_query_dict.lists() for v in l if (v is not None)])
 new = urlparse.ParseResult(url_.scheme, url_.netloc, url_.path, url_.params, query_string, fragment)
 return new.geturl()

def urlparams ( url_ , fragment = None , query_dict = None , ** query ) : url_ = urlparse . urlparse ( url_ ) fragment = ( fragment if ( fragment is not None ) else url_ . fragment ) q = url_ . query new_query_dict = ( QueryDict ( smart_str ( q ) , mutable = True ) if q else QueryDict ( '' , mutable = True ) ) if query_dict : for ( k , l ) in query_dict . lists ( ) : new_query_dict [ k ] = None for v in l : new_query_dict . appendlist ( k , v ) for ( k , v ) in query . items ( ) : if isinstance ( v , list ) : new_query_dict . setlist ( k , v ) else : new_query_dict [ k ] = v query_string = urlencode ( [ ( k , v ) for ( k , l ) in new_query_dict . lists ( ) for v in l if ( v is not None ) ] ) new = urlparse . ParseResult ( url_ . scheme , url_ . netloc , url_ . path , url_ . params , query_string , fragment ) return new . geturl ( )
add a fragment and/or query parameters to a url .