|
| 1 | +title: django.urls.exceptions NoReverseMatch Python Code Examples |
| 2 | +category: page |
| 3 | +slug: django-urls-exceptions-noreversematch-examples |
| 4 | +sortorder: 50056 |
| 5 | +toc: False |
| 6 | +sidebartitle: django.urls.exceptions NoReverseMatch |
| 7 | +meta: Python example code for the NoReverseMatch exception class from the django.urls.exceptions module. |
| 8 | + |
| 9 | + |
| 10 | +[NoReverseMatch](https://docs.djangoproject.com/en/dev/ref/exceptions/#noreversematch) |
| 11 | +([source code](https://github.com/django/django/blob/b9cf764be62e77b4777b3a75ec256f6209a57671/django/urls/exceptions.py)) |
| 12 | +is a [Django](/django.html) exception that is raised when a URL |
| 13 | +cannot be matched against any string or regular express in your URL |
| 14 | +configuration. |
| 15 | + |
| 16 | +A URL matching problem is often caused by missing arguments or |
| 17 | +supplying too many arguments. For example, let's say you have a blog |
| 18 | +project with URLs like "myblog.com/2019/10/title-slug", where `2019` |
| 19 | +is the year, `10` is the month and `title-slug` is the article's title |
| 20 | +as a [slug](https://stackoverflow.com/questions/427102/what-is-a-slug-in-django). |
| 21 | +A miss could happen if you have a URL configuration that is trying to |
| 22 | +find a blog post with the year and the month in the path, but your |
| 23 | +application only specifies the year without the month. |
| 24 | + |
| 25 | + |
| 26 | +## Example 1 from django-angular |
| 27 | +[django-angular](https://github.com/jrief/django-angular) |
| 28 | +([project examples website](https://django-angular.awesto.com/classic_form/)) |
| 29 | +is a library with helper code to make it easier to use |
| 30 | +[Angular](/angular.html) as the front-end to [Django](/django.html) projects. |
| 31 | +The code for django-angular is |
| 32 | +[open source under the MIT license](https://github.com/jrief/django-angular/blob/master/LICENSE.txt). |
| 33 | + |
| 34 | + |
| 35 | +[**django-angular / djng / core / urlresolvers.py**](https://github.com/jrief/django-angular/blob/master/djng/core/urlresolvers.py) |
| 36 | + |
| 37 | + |
| 38 | +```python |
| 39 | +# -*- coding: utf-8 -*- |
| 40 | +from __future__ import unicode_literals |
| 41 | +from inspect import isclass |
| 42 | + |
| 43 | +from django.utils import six |
| 44 | +~~from django.urls import (get_resolver, get_urlconf, resolve, |
| 45 | +~~ reverse, NoReverseMatch) |
| 46 | +from django.core.exceptions import ImproperlyConfigured |
| 47 | + |
| 48 | +try: |
| 49 | + from django.utils.module_loading import import_string |
| 50 | +except ImportError: |
| 51 | + from django.utils.module_loading \ |
| 52 | + import import_by_path as import_string |
| 53 | + |
| 54 | +from djng.views.mixins import JSONResponseMixin |
| 55 | + |
| 56 | + |
| 57 | +def _get_remote_methods_for(view_object, url): |
| 58 | + # view_object can be a view class or instance |
| 59 | + result = {} |
| 60 | + for field in dir(view_object): |
| 61 | + member = getattr(view_object, field, None) |
| 62 | + if callable(member) and hasattr(member, 'allow_rmi'): |
| 63 | + config = { |
| 64 | + 'url': url, |
| 65 | + 'method': getattr(member, 'allow_rmi'), |
| 66 | + 'headers': {'DjNg-Remote-Method': field}, |
| 67 | + } |
| 68 | + result.update({field: config}) |
| 69 | + return result |
| 70 | + |
| 71 | + |
| 72 | +def get_all_remote_methods(resolver=None, ns_prefix=''): |
| 73 | + """ |
| 74 | + Returns a dictionary to be used for calling |
| 75 | + ``djangoCall.configure()``, which itself extends the |
| 76 | + Angular API to the client, offering him to call remote methods. |
| 77 | + """ |
| 78 | + if not resolver: |
| 79 | + resolver = get_resolver(get_urlconf()) |
| 80 | + result = {} |
| 81 | + for name in resolver.reverse_dict.keys(): |
| 82 | + if not isinstance(name, six.string_types): |
| 83 | + continue |
| 84 | +~~ try: |
| 85 | +~~ url = reverse(ns_prefix + name) |
| 86 | +~~ resmgr = resolve(url) |
| 87 | +~~ ViewClass = import_string('{0}.{1}'.format(\ |
| 88 | +~~ resmgr.func.__module__, resmgr.func.__name__)) |
| 89 | +~~ if isclass(ViewClass) and issubclass(ViewClass, |
| 90 | +~~ JSONResponseMixin): |
| 91 | +~~ result[name] = _get_remote_methods_for(ViewClass, |
| 92 | +~~ url) |
| 93 | +~~ except (NoReverseMatch, ImproperlyConfigured): |
| 94 | +~~ pass |
| 95 | + for namespace, ns_pattern in resolver.namespace_dict.items(): |
| 96 | + sub_res = get_all_remote_methods(ns_pattern[1], |
| 97 | + ns_prefix + namespace + ':') |
| 98 | + if sub_res: |
| 99 | + result[namespace] = sub_res |
| 100 | + return result |
| 101 | + |
| 102 | + |
| 103 | +def get_current_remote_methods(view): |
| 104 | + if isinstance(view, JSONResponseMixin): |
| 105 | + return _get_remote_methods_for(view, |
| 106 | + view.request.path_info) |
| 107 | +``` |
0 commit comments