Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8178725
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T23:50:00+00:00 2026-06-06T23:50:00+00:00

When I read Django code sometimes, I see in some templates reverse() . I

  • 0

When I read Django code sometimes, I see in some templates reverse(). I am not quite sure what this is but it is used together with HttpResponseRedirect. How and when is this reverse() supposed to be used?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T23:50:01+00:00Added an answer on June 6, 2026 at 11:50 pm

    reverse() | Django documentation


    Let’s suppose that in your urls.py you have defined this:

    url(r'^foo$', some_view, name='url_name'),
    

    In a template you can then refer to this url as:

    <!-- django <= 1.4 -->
    <a href="{% url url_name %}">link which calls some_view</a>
    
    <!-- django >= 1.5 or with {% load url from future %} in your template -->
    <a href="{% url 'url_name' %}">link which calls some_view</a>
    

    This will be rendered as:

    <a href="/foo/">link which calls some_view</a>
    

    Now say you want to do something similar in your views.py – e.g. you are handling some other URL (not /foo/) in some other view (not some_view) and you want to redirect the user to /foo/ (often the case on successful form submission).

    You could just do:

    return HttpResponseRedirect('/foo/')
    

    But what if you want to change the URL in the future? You’d have to update your urls.py and all references to it in your code. This violates the DRY (Don’t Repeat Yourself) principle and the whole idea of editing in one place only – which is something to strive for.

    Instead, you can say:

    from django.http import HttpResponseRedirect
    from django.urls import reverse
    return HttpResponseRedirect(reverse('url_name'))
    

    This looks through all URLs defined in your project for the URL defined with the name url_name and returns the actual URL /foo/.

    This means that you refer to the URL only by its name attribute – if you want to change the URL itself or the view it refers to you can do this by editing one place only – urls.py.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

When I read django code sometimes, I see in some templates load url from
When I read Django code I often see in models what is called a
When I read django code I often see in models what is called sku,
Read on before you say this is a duplicate, it's not. (as far as
I'm test building a scraping site with django. For some reason the following code
I could have sworn I’d read a question about this before, but I can’t
I have read the Django documentation (For reference, https://docs.djangoproject.com/en/1.3/intro/tutorial01/ ), but I'm still having
Everybody in Django world seems to hate threadlocals( http://code.djangoproject.com/ticket/4280 , http://code.djangoproject.com/wiki/CookBookThreadlocalsAndUser ). I read
I've had a read of this http://docs.b-list.org/django-registration/0.8/backend-api.html and i've had a shot at making
I read a tutorial in Django site but I don't understand how to use

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.