Skip to content

Commit e7ff7ba

Browse files
committed
add new django code examples
1 parent 6ed921a commit e7ff7ba

File tree

5 files changed

+68
-2
lines changed

5 files changed

+68
-2
lines changed

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,9 @@ 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)
77+
* [django.conf.urls url](/django-conf-urls-url-examples.html)
7878
* [django.conf settings](/django-conf-settings-examples.html)
79+
* [django.http.responses HttpResponsePermanentRedirect](/django-http-responses-httpresponsepermanentredirect-examples.html)
7980
* [django.utils.html format_html](/django-utils-html-format-html-examples.html)
8081
* [django.urls.exceptions NoReverseMatch](/django-urls-exceptions-noreversematch-examples.html)
8182

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ a Django app.
2424
[Angular](/angular.html) web application that is open source under the
2525
[Apache2 license](https://github.com/mik4el/gadget-board/blob/master/LICENSE).
2626

27-
[**gadget-board/web/gadget_board_backend/urls.py**](https://github.com/mik4el/gadget-board/blob/master/web/gadget_board_backend/urls.py)
27+
[**gadget-board / web / gadget_board_backend / urls.py**](https://github.com/mik4el/gadget-board/blob/master/web/gadget_board_backend/urls.py)
2828

2929
```python
3030
from django.contrib import admin
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
title: django.http.responses HttpResponsePermanentRedirect Python Code Examples
2+
category: page
3+
slug: django-http-responses-httpresponsepermanentredirect-examples
4+
sortorder: 50057
5+
toc: False
6+
sidebartitle: django.http.responses HttpResponsePermanentRedirect
7+
meta: Example code that shows you how to use the HttpResponsePermanentRedirect class from the django.http.response module.
8+
9+
10+
[HttpResponsePermanentRedirect](https://docs.djangoproject.com/en/dev/ref/request-response/#django.http.HttpResponsePermanentRedirect)
11+
([source code](https://github.com/django/django/blob/master/django/http/response.py))
12+
is a class in the [Django](/django.html) code base for returning an
13+
[HTTP 301 status code](https://blog.hubspot.com/blog/tabid/6307/bid/7430/what-is-a-301-redirect-and-why-should-you-care.aspx)
14+
or a permanent URL redirect from your web application.
15+
16+
Note that you can import `HttpResponsePermanentRedirect` from either
17+
`django.http.responses` or `django.http`, because the latter one
18+
imports the responses from the `responses.py` file.
19+
20+
`HttpResponsePermanentRedirect` is often used in combination with
21+
[django.conf.urls url](/django-conf-urls-url-examples.html).
22+
23+
24+
## Example 1 from django-angular
25+
[django-angular](https://github.com/jrief/django-angular)
26+
([project examples website](https://django-angular.awesto.com/classic_form/))
27+
is a library with helper code to make it easier to use
28+
[Angular](/angular.html) as the front-end to [Django](/django.html) projects.
29+
The code for django-angular is open source under
30+
[the MIT license](https://github.com/jrief/django-angular/blob/master/LICENSE.txt).
31+
32+
[**django-angular / djng / urls.py**](https://github.com/jrief/django-angular/blob/master/djng/urls.py)
33+
34+
```python
35+
import warnings
36+
from django.urls import reverse
37+
from django.conf.urls import url
38+
~~from django.http.response import HttpResponsePermanentRedirect
39+
40+
41+
warnings.warn("Reversing URL's using urlpatterns is deprecated. "
42+
"Please use the middleware instead",
43+
DeprecationWarning)
44+
45+
46+
def angular_reverse(request, *args, **kwargs):
47+
url_name = request.GET.get('djng_url_name')
48+
url_args = request.GET.getlist('djng_url_args', None)
49+
url_kwargs = {}
50+
51+
prefix = 'djng_url_kwarg_'
52+
for param in request.GET:
53+
if param.startswith(prefix):
54+
url_kwargs[param[len(prefix):]] = request.GET[param]
55+
56+
~~ url = reverse(url_name, args=url_args, kwargs=url_kwargs)
57+
~~ return HttpResponsePermanentRedirect(url)
58+
59+
60+
urlpatterns = [
61+
url(r'^reverse/$', angular_reverse),
62+
]
63+
```

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.http.responses HttpResponsePermanentRedirect](/django-http-responses-httpresponsepermanentredirect-examples.html)
3132
* [django.urls.exceptions NoReverseMatch](/django-urls-exceptions-noreversematch-examples.html)
3233
* Updated code examples on the following pages:
3334
* [django.contrib admin](/django-contrib-admin-examples.html)

theme/templates/table-of-contents.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,7 @@ <h4 class="bp"><a href="/django-db-models-signal-examples.html">django.db.models
250250
<h4 class="bp"><a href="/django-dispatch-dispatcher-signal-examples.html">django.dispatch.dispatcher Signal</a></h4>
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>
253+
<h4 class="bp"><a href="/django-http-responses-httpresponsepermanentredirect-examples.html">django.http.responses HttpResponsePermanentRedirect</a></h4>
253254
<h4 class="bp"><a href="/django-urls-path-examples.html">django.urls.path</a></h4>
254255
<h4 class="bp"><a href="/django-urls-exceptions-noreversematch-examples.html">django.urls.exceptions NoReverseMatch</a></h4>
255256
<h4 class="bp"><a href="/django-utils-html-format-html-examples.html">django.utils.html format_html</a></h4>

0 commit comments

Comments
 (0)