|
| 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 | +``` |
0 commit comments