我们从Python开源项目中,提取了以下50个代码示例,用于说明如何使用django.utils.http.urlencode()。
def test_restore_backup(self): backup = self.cinder_volume_backups.first() volumes = self.cinder_volumes.list() api.cinder.volume_list(IsA(http.HttpRequest)). \ AndReturn(volumes) api.cinder.volume_backup_restore(IsA(http.HttpRequest), backup.id, backup.volume_id). \ AndReturn(backup) self.mox.ReplayAll() formData = {'method': 'RestoreBackupForm', 'backup_id': backup.id, 'backup_name': backup.name, 'volume_id': backup.volume_id} url = reverse('horizon:project:volumes:backups:restore', args=[backup.id]) url += '?%s' % urlencode({'backup_name': backup.name, 'volume_id': backup.volume_id}) res = self.client.post(url, formData) self.assertNoFormErrors(res) self.assertMessageCount(info=1) self.assertRedirectsNoFollow(res, INDEX_URL)
def test_associate_with_instance_id(self): api.network.floating_ip_target_list(IsA(http.HttpRequest)) \ .AndReturn(self.servers.list()) api.network.floating_ip_target_get_by_instance( IsA(http.HttpRequest), 'TEST-ID', self.servers.list()) \ .AndReturn('TEST-ID') api.network.tenant_floating_ip_list(IsA(http.HttpRequest)) \ .AndReturn(self.floating_ips.list()) self.mox.ReplayAll() base_url = reverse('%s:associate' % NAMESPACE) params = urlencode({'instance_id': 'TEST-ID'}) url = '?'.join([base_url, params]) res = self.client.get(url) self.assertTemplateUsed(res, views.WorkflowView.template_name) workflow = res.context['workflow'] choices = dict(workflow.steps[0].action.fields['ip_id'].choices) # Verify that our "associated" floating IP isn't in the choices list. self.assertTrue(self.floating_ips.first() not in choices)
def test_associate_with_port_id(self): targets = [api.nova.FloatingIpTarget(s) for s in self.servers.list()] targets[0].port_id = '101' api.network.floating_ip_target_list(IsA(http.HttpRequest)) \ .AndReturn(targets) api.network.tenant_floating_ip_list(IsA(http.HttpRequest)) \ .AndReturn(self.floating_ips.list()) self.mox.ReplayAll() base_url = reverse('%s:associate' % NAMESPACE) params = urlencode({'port_id': '101'}) url = '?'.join([base_url, params]) res = self.client.get(url) self.assertTemplateUsed(res, views.WorkflowView.template_name) workflow = res.context['workflow'] choices = dict(workflow.steps[0].action.fields['ip_id'].choices) # Verify that our "associated" floating IP isn't in the choices list. self.assertTrue(self.floating_ips.first() not in choices)
def get_query_string(self, new_params=None, remove=None): if new_params is None: new_params = {} if remove is None: remove = [] p = self.params.copy() for r in remove: for k in list(p): if k.startswith(r): del p[k] for k, v in new_params.items(): if v is None: if k in p: del p[k] else: p[k] = v return '?%s' % urlencode(sorted(p.items()))
def get_preserved_filters(self, request): """ Returns the preserved filters querystring. """ match = request.resolver_match if self.preserve_filters and match: opts = self.model._meta current_url = '%s:%s' % (match.app_name, match.url_name) changelist_url = 'admin:%s_%s_changelist' % (opts.app_label, opts.model_name) if current_url == changelist_url: preserved_filters = request.GET.urlencode() else: preserved_filters = request.GET.get('_changelist_filters') if preserved_filters: return urlencode({'_changelist_filters': preserved_filters}) return ''
def get_query_string(self, new_params=None, remove=None): if new_params is None: new_params = {} if remove is None: remove = [] p = dict(self.request.GET.items()).copy() arr_keys = list(p.keys()) for r in remove: for k in arr_keys: if k.startswith(r): del p[k] for k, v in new_params.items(): if v is None: if k in p: del p[k] else: p[k] = v return '?%s' % urlencode(p)
def get_query_string(self, new_params=None, remove=None): if new_params is None: new_params = {} if remove is None: remove = [] p = dict(self.request.GET.items()).copy() for r in remove: for k in p.keys(): if k.startswith(r): del p[k] for k, v in new_params.items(): if v is None: if k in p: del p[k] else: p[k] = v return '?%s' % urlencode(p)
def login(request): app = providers.registry.by_id( DraugiemProvider.id, request).get_app(request) request_scheme = request.META['wsgi.url_scheme'] request_host = request.META['HTTP_HOST'] request_path = reverse(callback) redirect_url = '%s://%s%s' % (request_scheme, request_host, request_path) redirect_url_hash = md5(( app.secret + redirect_url).encode('utf-8')).hexdigest() params = { 'app': app.client_id, 'hash': redirect_url_hash, 'redirect': redirect_url, } SocialLogin.stash_state(request) return HttpResponseRedirect('%s?%s' % (AUTHORIZE_URL, urlencode(params)))
def _get_request_token(self): """ Obtain a temporary request token to authorize an access token and to sign the request to obtain the access token """ if self.request_token is None: get_params = {} if self.parameters: get_params.update(self.parameters) get_params['oauth_callback'] \ = self.request.build_absolute_uri(self.callback_url) rt_url = self.request_token_url + '?' + urlencode(get_params) oauth = OAuth1(self.consumer_key, client_secret=self.consumer_secret) response = requests.post(url=rt_url, auth=oauth) if response.status_code not in [200, 201]: raise OAuthError( _('Invalid response while obtaining request token' ' from "%s".') % get_token_prefix( self.request_token_url)) self.request_token = dict(parse_qsl(response.text)) self.request.session['oauth_%s_request_token' % get_token_prefix( self.request_token_url)] = self.request_token return self.request_token
def get_query_string(self, new_params=None, remove=None): """ URL ???? ????query_string???????query_string :param new_params: ??????????? dict :param remove: ??????????? list, tuple """ if new_params is None: new_params = {} if remove is None: remove = [] p = dict(self.request.GET.items()).copy() for r in remove: for k in p.keys(): if k.startswith(r): del p[k] for k, v in new_params.items(): if v is None: if k in p: del p[k] else: p[k] = v return '?%s' % urlencode(p)
def get_preserved_filters(self, request): """ Returns the preserved filters querystring. """ match = request.resolver_match if self.preserve_filters and match: opts = self._meta info = (self._meta.app_label, self.block_type_name) changelist_name = '%s_%s_changelist' % info current_url = '%s:%s' % (match.app_name, match.url_name) changelist_url = 'admin:%s' % changelist_name if current_url == changelist_url: preserved_filters = request.GET.urlencode() else: preserved_filters = request.GET.get('_changelist_filters') if preserved_filters: return urlencode({'_changelist_filters': preserved_filters}) return ''
def render(self, context): if 'request' not in context: raise ImproperlyConfigured(context_processor_error_msg % 'querystring') params = dict(context['request'].GET) for key, value in self.updates.items(): key = key.resolve(context) value = value.resolve(context) if key not in ('', None): params[key] = value for removal in self.removals: params.pop(removal.resolve(context), None) return escape('?' + urlencode(params, doseq=True)) # {% querystring "name"="abc" "age"=15 %}
def get_query_string(self, new_params=None, remove=None): if new_params is None: new_params = {} if remove is None: remove = [] p = dict(list(self.request.GET.items())).copy() for r in remove: for k in list(p.keys()): if k.startswith(r): del p[k] for k, v in list(new_params.items()): if v is None: if k in p: del p[k] else: p[k] = v return '?%s' % urlencode(p)