我们从Python开源项目中,提取了以下4个代码示例,用于说明如何使用rest_framework.urlpatterns.format_suffix_patterns()。
def get_urls(self): """ Generate the list of URL patterns, including a default root view for the API, and appending `.json` style format suffixes. """ urls = super(DefaultRouter, self).get_urls() if self.include_root_view: if self.schema_title: view = self.get_schema_root_view(api_urls=urls) else: view = self.get_api_root_view(api_urls=urls) root_url = url(r'^$', view, name=self.root_view_name) urls.append(root_url) if self.include_format_suffixes: urls = format_suffix_patterns(urls) return urls
def get_urls(self): """ Generate the list of URL patterns, including a default root view for the API, and appending `.json` style format suffixes. """ urls = [] if self.include_root_view: root_url = url(r'^$', self.get_api_root_view(), name=self.root_view_name) urls.append(root_url) default_urls = super(DefaultRouter, self).get_urls() urls.extend(default_urls) if self.include_format_suffixes: urls = format_suffix_patterns(urls) return urls
def get_urls(self): ret = [] if self.include_root_view: root_url = url(r'^$', self.get_api_root_view(), name=self.root_view_name) ret.append(root_url) for prefix, viewset, basename in self.registry: lookup = self.get_lookup_regex(viewset) lookup_list = self.get_lookup_regexes(viewset) if lookup_list: # lookup = lookups[0] lookup_list = [u'/'.join(k) for k in lookup_list] else: lookup_list = [u''] routes = self.get_routes(viewset) for route in routes: mapping = self.get_method_map(viewset, route.mapping) if not mapping: continue for lookups in lookup_list: regex = route.url.format( prefix=prefix, lookup=lookup, lookups=lookups, trailing_slash=self.trailing_slash, extra=self.get_extra_lookup_regexes(route) ) view = viewset.as_view(mapping, **route.initkwargs) name = route.name.format(basename=basename) ret.append(url(regex, view, name=name)) if self.include_format_suffixes: ret = format_suffix_patterns(ret, allowed=['[a-z0-9]+']) return ret
def get_urls(self): ret = [] if self.include_root_view: root_url = url(r'^$', self.get_api_root_view(), name=self.root_view_name) ret.append(root_url) for prefix, viewset, basename in self.registry: lookup = self.get_lookup_regex(viewset) lookup_list = self.get_lookup_regexes(viewset) if lookup_list: # lookup = lookups[0] lookup_list = [u'/'.join(k) for k in lookup_list] else: lookup_list = [u''] routes = self.get_routes(viewset) for route in routes: mapping = self.get_method_map(viewset, route.mapping) if not mapping: continue for lookups in lookup_list: regex = route.url.format( prefix=prefix, lookup=lookup, lookups=lookups, trailing_slash=self.trailing_slash, extra=self.get_extra_lookup_regexes(route) ) view = viewset.as_view(mapping, **route.initkwargs) name = route.name.format(basename=basename) ret.append(url(regex, view, name=name)) if self.include_format_suffixes: ret = format_suffix_patterns(ret, allowed=['[a-z]+[0-9]*']) return ret