Q: How to use Django reverse urls in views?

Django Reverse urls Python

Answer:

To use Django reverse urls, you must first give each url an unique name. For example:

url(r'^blog/$', views.blog, name='blog_index'),

Now in your views, you can do the following:

from django.core.urlresolvers import reverse
from django.http import HttpResponsePermanentRedirect


def redirect_to_blog(request):
    # This will redirect requests to blog url.
    return HttpResponsePermanentRedirect(reverse('blog_index'))

If you have url arguments, you can use:

reverse('blog', args=[blog_id])
    No comments found for this tutorial, be the first to leave a comment!

Answered by
Double

Last updated on
Aug 15, 2016

Share