Skip to content

Commit 8467443

Browse files
committed
working on noreversematch exception django code examples page
1 parent 92c448d commit 8467443

File tree

5 files changed

+111
-1
lines changed

5 files changed

+111
-1
lines changed

content/pages/examples/django/django-code-examples.markdown

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ The code for django-angular is
7575
Code from django-angular is shown on:
7676

7777
* [django.utils.html format_html](/django-utils-html-format-html-examples.html)
78+
* [django.urls.exceptions NoReverseMatch](/django-urls-exceptions-noreversematch-examples.html)
7879

7980

8081
### django-cors-headers
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
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+
```

content/pages/meta/00-change-log.markdown

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ on GitHub.
2828
* [django.contrib.auth.decorators login_required](/django-contrib-auth-decorators-login-required-examples.html)
2929
* [django.contrib.auth.hashers make_password](/django-contrib-auth-hashers-make-password-examples.html)
3030
* [django.http Http404](/django-http-http404-examples.html)
31+
* [django.urls.exceptions NoReverseMatch](/django-urls-exceptions-noreversematch-examples.html)
3132
* Updated code examples on the following pages:
3233
* [django.contrib admin](/django-contrib-admin-examples.html)
3334
* [django.db.models DateTimeField](/django-db-models-datetimefield-examples.html)

theme/templates/base.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
{% block content %}{% endblock %}
1717
<hr></div>
1818
{% block lower_banner %}{% endblock %}
19-
<div class="cn"><div class="ft sns"><a href="/about-author.html">Matt Makai</a> <a href="/change-log.html">2012-2019</a></div></div>
19+
<div class="cn"><div class="ft sns"><a href="/about-author.html">Matt Makai</a> <a href="/change-log.html">2012-2019</a> | <a href="https://g.ezoic.net/privacy/fullstackpython.com">Privacy Policy</a></div></div>
2020
{% block bottom_banner %}{% endblock %}
2121
<script>(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){ (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o), m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m) })(window,document,'script','//www.google-analytics.com/analytics.js','ga'); ga('create', 'UA-19910497-7', 'auto'); ga('send', 'pageview');</script>
2222
{% block js %}{% endblock %}

theme/templates/table-of-contents.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,7 @@ <h4 class="bp"><a href="/django-dispatch-dispatcher-signal-examples.html">django
251251
<h4 class="bp"><a href="/django-forms-examples.html">django.forms</a></h4>
252252
<h4 class="bp"><a href="/django-http-http404-examples.html">django.http Http404</a></h4>
253253
<h4 class="bp"><a href="/django-urls-path-examples.html">django.urls.path</a></h4>
254+
<h4 class="bp"><a href="/django-urls-exceptions-noreversematch-examples.html">django.urls.exceptions NoReverseMatch</a></h4>
254255
<h4 class="bp"><a href="/django-utils-html-format-html-examples.html">django.utils.html format_html</a></h4>
255256
<h4 class="bp"><a href="/django-utils-timezone-examples.html">django.utils.timezone</a></h4>
256257
</div>

0 commit comments

Comments
 (0)