我们从Python开源项目中,提取了以下50个代码示例,用于说明如何使用django.utils.six.moves.urllib.parse.urlunparse()。
def url_replace_param(url, name, value): """ Replace a GET parameter in an URL """ url_components = urlparse(force_str(url)) params = parse_qs(url_components.query) if value is None: del params[name] else: params[name] = value return mark_safe(urlunparse([ url_components.scheme, url_components.netloc, url_components.path, url_components.params, urlencode(params, doseq=True), url_components.fragment, ]))
def redirect_to_login(next, login_url=None, redirect_field_name=REDIRECT_FIELD_NAME): """ Redirects the user to the login page, passing the given 'next' page """ resolved_url = resolve_url(login_url or settings.LOGIN_URL) login_url_parts = list(urlparse(resolved_url)) if redirect_field_name: querystring = QueryDict(login_url_parts[4], mutable=True) querystring[redirect_field_name] = next login_url_parts[4] = querystring.urlencode(safe='/') return HttpResponseRedirect(urlunparse(login_url_parts)) # 4 views for password reset: # - password_reset sends the mail # - password_reset_done shows a success message for the above # - password_reset_confirm checks the link the user clicked and # prompts for a new password # - password_reset_complete shows a success message for the above
def add_preserved_filters(context, url, popup=False, to_field=None): opts = context.get('opts') preserved_filters = context.get('preserved_filters') parsed_url = list(urlparse(url)) parsed_qs = dict(parse_qsl(parsed_url[4])) merged_qs = dict() if opts and preserved_filters: preserved_filters = dict(parse_qsl(preserved_filters)) match_url = '/%s' % url.partition(get_script_prefix())[2] try: match = resolve(match_url) except Resolver404: pass else: current_url = '%s:%s' % (match.app_name, match.url_name) changelist_url = 'admin:%s_%s_changelist' % (opts.app_label, opts.model_name) if changelist_url == current_url and '_changelist_filters' in preserved_filters: preserved_filters = dict(parse_qsl(preserved_filters['_changelist_filters'])) merged_qs.update(preserved_filters) if popup: from django.contrib.admin.options import IS_POPUP_VAR merged_qs[IS_POPUP_VAR] = 1 if to_field: from django.contrib.admin.options import TO_FIELD_VAR merged_qs[TO_FIELD_VAR] = to_field merged_qs.update(parsed_qs) parsed_url[4] = urlencode(merged_qs) return urlunparse(parsed_url)
def redirect_to_sso(self, next_url): """ Redirects the user to the sso signup page, passing the 'next' page """ resolved_url = resolve_url(self.sso_redirect_url) login_url_parts = list(urlparse(resolved_url)) querystring = QueryDict(login_url_parts[4], mutable=True) querystring[settings.SSO_PROXY_REDIRECT_FIELD_NAME] = next_url login_url_parts[4] = querystring.urlencode(safe='/') return HttpResponseRedirect(urlunparse(login_url_parts))
def modify_url(old_path, new_path='', carry_params=True, new_params=None): """ Returns a new path based on the following conditions if new_path is not given old path is returned modified as per the given arguments if new_path if given new path is returned modified as per the given arguments :param old_path: default values are taken from this path :param new_path: path to be modified :param carry_params: Carry forwards the query params from old path :param new_params: Appends the given query params :return: url path """ if not(old_path or new_path): return old_path old_url_parts = list(urlparse(old_path)) new_url_parts = list(urlparse(new_path)) query_params = old_url_parts[-2] if carry_params else None query_dict = QueryDict(query_params, mutable=True) # XXX QueryDict.update does not replace the key but appends the value for key, list_ in QueryDict(new_url_parts[-2]).lists(): query_dict.setlist(key, list_) if new_params: # assumed to be urlencoded string for key, list_ in QueryDict(new_params).lists(): query_dict.setlist(key, list_) new_url_parts[-2] = query_dict.urlencode() new_url_parts[2] = new_url_parts[2] or old_url_parts[2] return urlunparse(new_url_parts)