Skip to content

Commit 6ed921a

Browse files
committed
add new urls example code for django
1 parent 362ab8e commit 6ed921a

File tree

2 files changed

+43
-0
lines changed

2 files changed

+43
-0
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ The code for django-angular is
7474

7575
Code from django-angular is shown on:
7676

77+
* [django-conf-urls url](/django-conf-urls-url-examples.html)
7778
* [django.conf settings](/django-conf-settings-examples.html)
7879
* [django.utils.html format_html](/django-utils-html-format-html-examples.html)
7980
* [django.urls.exceptions NoReverseMatch](/django-urls-exceptions-noreversematch-examples.html)

content/pages/examples/django/django-conf-urls-url.markdown

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -297,3 +297,45 @@ urlpatterns = [
297297
~~ {'post_reset_redirect': '/accounts/password/done/'},),
298298
]
299299
```
300+
301+
302+
## Example 7 from django-angular
303+
[django-angular](https://github.com/jrief/django-angular)
304+
([project examples website](https://django-angular.awesto.com/classic_form/))
305+
is a library with helper code to make it easier to use
306+
[Angular](/angular.html) as the front-end to [Django](/django.html) projects.
307+
The code for django-angular is
308+
[open source under the MIT license](https://github.com/jrief/django-angular/blob/master/LICENSE.txt).
309+
310+
[**django-angular / djng / urls.py**](https://github.com/jrief/django-angular/blob/master/djng/urls.py)
311+
312+
```python
313+
import warnings
314+
from django.urls import reverse
315+
~~from django.conf.urls import url
316+
from django.http.response import HttpResponsePermanentRedirect
317+
318+
319+
warnings.warn("Reversing URL's using urlpatterns is deprecated. "
320+
"Please use the middleware instead",
321+
DeprecationWarning)
322+
323+
324+
def angular_reverse(request, *args, **kwargs):
325+
url_name = request.GET.get('djng_url_name')
326+
url_args = request.GET.getlist('djng_url_args', None)
327+
url_kwargs = {}
328+
329+
prefix = 'djng_url_kwarg_'
330+
for param in request.GET:
331+
if param.startswith(prefix):
332+
url_kwargs[param[len(prefix):]] = request.GET[param]
333+
334+
url = reverse(url_name, args=url_args, kwargs=url_kwargs)
335+
return HttpResponsePermanentRedirect(url)
336+
337+
338+
urlpatterns = [
339+
~~ url(r'^reverse/$', angular_reverse),
340+
]
341+
```

0 commit comments

Comments
 (0)