HttpResponsePermanentRedirect (source code) is a class in the Django code base for returning an HTTP 301 status code or a permanent URL redirect from your web application.
Note that you can import HttpResponsePermanentRedirect
from either
django.http.responses
or django.http
, because the latter one
imports the responses from the responses.py
file.
HttpResponsePermanentRedirect
is often used in combination with
django.conf.urls url.
django-angular (project examples website) is a library with helper code to make it easier to use Angular as the front-end to Django projects. The code for django-angular is open source under the MIT license.
django-angular / djng / urls.py
import warnings
from django.urls import reverse
from django.conf.urls import url
from django.http.response import HttpResponsePermanentRedirect
warnings.warn("Reversing URL's using urlpatterns is deprecated. "
"Please use the middleware instead",
DeprecationWarning)
def angular_reverse(request, *args, **kwargs):
url_name = request.GET.get('djng_url_name')
url_args = request.GET.getlist('djng_url_args', None)
url_kwargs = {}
prefix = 'djng_url_kwarg_'
for param in request.GET:
if param.startswith(prefix):
url_kwargs[param[len(prefix):]] = request.GET[param]
url = reverse(url_name, args=url_args, kwargs=url_kwargs)
return HttpResponsePermanentRedirect(url)
urlpatterns = [
url(r'^reverse/$', angular_reverse),
]