我们从Python开源项目中,提取了以下50个代码示例,用于说明如何使用django.urls.NoReverseMatch()。
def _get_sitemap_full_url(sitemap_url): if not django_apps.is_installed('django.contrib.sites'): raise ImproperlyConfigured("ping_google requires django.contrib.sites, which isn't installed.") if sitemap_url is None: try: # First, try to get the "index" sitemap URL. sitemap_url = reverse('django.contrib.sitemaps.views.index') except NoReverseMatch: try: # Next, try for the "global" sitemap URL. sitemap_url = reverse('django.contrib.sitemaps.views.sitemap') except NoReverseMatch: pass if sitemap_url is None: raise SitemapNotFound("You didn't provide a sitemap_url, and the sitemap URL couldn't be auto-detected.") Site = django_apps.get_model('sites.Site') current_site = Site.objects.get_current() return 'http://%s%s' % (current_site.domain, sitemap_url)
def test_apis_url_exist(self): try: # PostList resolve('/api/post/') # PostDetail resolve('/api/post/1/') except NoReverseMatch as e: self.fail(e)
def ping_google(sitemap_url=None, ping_url=PING_URL): """ Alerts Google that the sitemap for the current site has been updated. If sitemap_url is provided, it should be an absolute path to the sitemap for this site -- e.g., '/sitemap.xml'. If sitemap_url is not provided, this function will attempt to deduce it by using urls.reverse(). """ if sitemap_url is None: try: # First, try to get the "index" sitemap URL. sitemap_url = reverse('django.contrib.sitemaps.views.index') except NoReverseMatch: try: # Next, try for the "global" sitemap URL. sitemap_url = reverse('django.contrib.sitemaps.views.sitemap') except NoReverseMatch: pass if sitemap_url is None: raise SitemapNotFound("You didn't provide a sitemap_url, and the sitemap URL couldn't be auto-detected.") if not django_apps.is_installed('django.contrib.sites'): raise ImproperlyConfigured("ping_google requires django.contrib.sites, which isn't installed.") Site = django_apps.get_model('sites.Site') current_site = Site.objects.get_current() url = "http://%s%s" % (current_site.domain, sitemap_url) params = urlencode({'sitemap': url}) urlopen("%s?%s" % (ping_url, params))
def get_admin_url(self): """ Returns the admin URL to edit the object represented by this log entry. """ if self.content_type and self.object_id: url_name = 'admin:%s_%s_change' % (self.content_type.app_label, self.content_type.model) try: return reverse(url_name, args=(quote(self.object_id),)) except NoReverseMatch: pass return None
def reverse_any(viewnames, urlconf=None, args=None, kwargs=None, *fargs, **fkwargs): """ Tries reversing a list of viewnames with the same arguments, and returns the first result where no ``NoReverseMatch`` exception is raised. Usage:: url = reverse_any(( 'blog:article-detail', 'articles:article-detail', ), kwargs={'slug': 'article-slug'}) """ for viewname in viewnames: try: return reverse(viewname, urlconf, args, kwargs, *fargs, **fkwargs) except NoReverseMatch: pass raise NoReverseMatch( "Reverse for any of '%s' with arguments '%s' and keyword arguments" " '%s' not found." % ( "', '".join(viewnames), args or [], kwargs or {}))
def reverse_fallback(fallback, fn, *args, **kwargs): """ Returns the result of ``fn(*args, **kwargs)``, or ``fallback`` if the former raises a ``NoReverseMatch`` exception. This is especially useful for reversing app URLs from outside the app and you do not want crashes if the app isn't available anywhere. The following two examples are equivalent, choose whichever you like best:: reverse_fallback('/', lambda: reverse_app( ('articles',), 'article-detail', kwargs={'slug': self.slug}, )) reverse_fallback( '/', reverse_app ('articles',), 'article-detail', kwargs={'slug': self.slug}, ) """ try: return fn(*args, **kwargs) except NoReverseMatch: return fallback
def message_to_host(event): try: api_link = reverse('event_review_host_message', args=[event.organization.slug, event.id, '']) return render_to_string( 'event_review/message_to_host_widget.html', {'event_id':event.id, 'link': api_link}) except NoReverseMatch: return None
def test_apis_url_exist(self): try: # 1. MapList? ?? url view? ????? ?? resolve('/api/map/') # 2. MapDetail? ?? url view? ????? ?? resolve('/api/map/1/') except NoReverseMatch as e: self.fail(e)
def resolve_url(to, *args, **kwargs): """ Return a URL appropriate for the arguments passed. The arguments could be: * A model: the model's `get_absolute_url()` function will be called. * A view name, possibly with arguments: `urls.reverse()` will be used to reverse-resolve the name. * A URL, which will be returned as-is. """ # If it's a model, use get_absolute_url() if hasattr(to, 'get_absolute_url'): return to.get_absolute_url() if isinstance(to, Promise): # Expand the lazy instance, as it can cause issues when it is passed # further to some Python functions like urlparse. to = force_text(to) if isinstance(to, six.string_types): # Handle relative URLs if to.startswith(('./', '../')): return to # Next try a reverse URL resolution. try: return reverse(to, args=args, kwargs=kwargs) except NoReverseMatch: # If this is a callable, re-raise. if callable(to): raise # If this doesn't "feel" like a URL, re-raise. if '/' not in to and '.' not in to: raise # Finally, fall back and assume it's a URL return to
def render(self, context): from django.urls import reverse, NoReverseMatch args = [arg.resolve(context) for arg in self.args] kwargs = { force_text(k, 'ascii'): v.resolve(context) for k, v in self.kwargs.items() } view_name = self.view_name.resolve(context) try: current_app = context.request.current_app except AttributeError: try: current_app = context.request.resolver_match.namespace except AttributeError: current_app = None # Try to look up the URL. If it fails, raise NoReverseMatch unless the # {% url ... as var %} construct is used, in which case return nothing. url = '' try: url = reverse(view_name, args=args, kwargs=kwargs, current_app=current_app) except NoReverseMatch: if self.asvar is None: raise if self.asvar: context[self.asvar] = url return '' else: if context.autoescape: url = conditional_escape(url) return url
def _get_url(self, item_dict): """ Given a menu item dictionary, it returns the URL or an empty string. """ url = item_dict.get('url', '') try: final_url = reverse(**url) if type(url) is dict else reverse(url) except NoReverseMatch: final_url = url return final_url
def change_url(widget, obj): if not widget.model and not isinstance(obj, models.Model): # No chance to get model url return elif isinstance(obj, collections.Mapping): pk = obj.get('pk', obj.get('id')) meta = widget.model._meta elif indexonly(obj): if not widget.list_display: # No chance to guess pk return # Excludes shart tip zip keys and values keys = (k for k in widget.list_display if k != app_settings.SHARP) new_obj = {x: y for x, y in zip(keys, obj)} return change_url(widget, new_obj) else: pk = getattr(obj, 'pk', getattr(obj, 'id', None)) if not isinstance(obj, models.Model): # Namedtuples and custom stuff meta = widget.model._meta elif getattr(obj, '_deferred', False): # Deffered model meta = obj._meta.proxy_for_model._meta else: # Regular model or django 1.10 deferred meta = obj._meta if pk is None: # No chance to get item url return try: app_label, model_name = meta.app_label, meta.model_name return reverse('admin:{}_{}_change'.format(app_label, model_name), args=[pk]) except NoReverseMatch: return
def _add_quote_tag(parser): def render_quote(tag_name, value, options, parent, context): author = options.get('author', None) pk = options.get('pk', None) if author: attribution = 'Originally posted by %s' % author if pk: try: url = reverse('post', kwargs={'post_id': pk}) except NoReverseMatch: pass else: attribution = """ Originally posted by %s <a class="jump-to-post" href="%s"></a> """ % (author, url) template = """ <blockquote> <small class="attribution">%s</small> %s </blockquote> """ return template % (attribution, value) else: return '<blockquote>%s</blockquote>' % value parser.add_formatter('quote', render_quote, strip=True, swallow_trailing_newline=True) return parser
def get_and_linkify_tags(self): """Find tags in text and convert them to Markdown links. Save found tags to the content. """ found_tags = set() lines = self.text.splitlines(keepends=True) final_words = [] code_block = False # Check each line separately for line in lines: if line[0:3] == "```": code_block = not code_block # noinspection PyTypeChecker if line.find("#") == -1 or line[0:4] == " " or code_block: # Just add the whole line final_words.append(line) continue # Check each word separately # noinspection PyTypeChecker words = line.split(" ") for word in words: # noinspection PyTypeChecker candidate = word.strip().strip("([]),.!?:") # noinspection PyTypeChecker if candidate.startswith("#"): # noinspection PyTypeChecker candidate = candidate.strip("#") if test_tag(candidate.lower()): # Tag found_tags.add(candidate.lower()) try: # noinspection PyTypeChecker tag_word = word.replace( "#%s" % candidate, "[#%s](%s)" % ( candidate, reverse("streams:tag", kwargs={"name": candidate.lower()}) ) ) final_words.append(tag_word) except NoReverseMatch: # Don't linkify, seems we can't generate an url for it # TODO: throw to opbeat or just logger? final_words.append(word) else: # Not tag final_words.append(word) else: final_words.append(word) text = " ".join(final_words) self.save_tags(found_tags) return text
def get_api_root_view(self, api_urls=None): api_root_dict = OrderedDict() list_name = self.routes[0].name for prefix, viewset, basename in self.registry: api_root_dict[prefix] = list_name.format(basename=basename) view_renderers = list(self.root_renderers) schema_media_types = [] if api_urls and self.schema_title: view_renderers += list(self.schema_renderers) schema_generator = SchemaGenerator( title=self.schema_title, url=self.schema_url, patterns=api_urls ) schema_media_types = [ renderer.media_type for renderer in self.schema_renderers ] class APIRoot(views.APIView): _ignore_model_permissions = True renderer_classes = view_renderers def get(self, request, *args, **kwargs): if request.accepted_renderer.media_type in schema_media_types: # Return a schema response. schema = schema_generator.get_schema(request) if schema is None: raise exceptions.PermissionDenied() return Response(schema) # Return a plain {"name": "hyperlink"} response. ret = OrderedDict() namespace = request.resolver_match.namespace for key, url_name in api_root_dict.items(): if namespace: url_name = namespace + ':' + url_name try: ret[key] = reverse( url_name, args=args, kwargs=kwargs, request=request, format=kwargs.get('format', None) ) except NoReverseMatch: # Don't bail out if eg. no list routes exist, # only detail routes. continue return Response(ret) return APIRoot.as_view()
def get_deleted_objects(objs, opts, user, admin_site, using): """ Find all objects related to ``objs`` that should also be deleted. ``objs`` must be a homogeneous iterable of objects (e.g. a QuerySet). Returns a nested list of strings suitable for display in the template with the ``unordered_list`` filter. """ collector = NestedObjects(using=using) collector.collect(objs) perms_needed = set() def format_callback(obj): has_admin = obj.__class__ in admin_site._registry opts = obj._meta no_edit_link = '%s: %s' % (capfirst(opts.verbose_name), force_text(obj)) if has_admin: try: admin_url = reverse('%s:%s_%s_change' % (admin_site.name, opts.app_label, opts.model_name), None, (quote(obj._get_pk_val()),)) except NoReverseMatch: # Change url doesn't exist -- don't display link to edit return no_edit_link p = '%s.%s' % (opts.app_label, get_permission_codename('delete', opts)) if not user.has_perm(p): perms_needed.add(opts.verbose_name) # Display a link to the admin page. return format_html('{}: <a href="{}">{}</a>', capfirst(opts.verbose_name), admin_url, obj) else: # Don't display link to edit, because it either has no # admin or is edited inline. return no_edit_link to_delete = collector.nested(format_callback) protected = [format_callback(obj) for obj in collector.protected] model_count = {model._meta.verbose_name_plural: len(objs) for model, objs in collector.model_objs.items()} return to_delete, model_count, perms_needed, protected