django.http.responses HttpResponsePermanentRedirect Python Code Examples

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 file.

HttpResponsePermanentRedirect is often used in combination with django.conf.urls url.

Example 1 from django-angular

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 /

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",

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),

Sign up for a monthly email with Full Stack Python tutorials. No spam ever.

Learn More Python

Introduction to Ansible video course logo.

Deploy web apps with the Ansible configuration management tool.

Full Stack Python

Full Stack Python is an open book that explains concepts in plain language and provides helpful resources for those topics.
Updates via newsletter, Twitter & Facebook.

Matt Makai 2012-2019