Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Web Technology by (19.9k points)

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?

It would nice if someone gave an answer with some examples...

1 Answer

0 votes
by (25.1k points)

Django provides tools for performing URL reversing that match the different layers where URLs are needed: In templates: Using the url template tag. In Python code: Using the reverse() function. In higher level code related to handling of URLs of Django model instances: The get_absolute_url() method.

Eg. in templates (url tag)

<a href="{% url'news-year-archive' 2012 %}">2012 Archive</a>

Eg. in python code (using the reverse function)

return HttpResponseRedirect(reverse('news-year-archive', args=(year,)))

Related questions

0 votes
1 answer
asked Oct 5, 2019 in Web Technology by Rajesh Malhotra (19.9k points)
0 votes
1 answer
0 votes
1 answer
asked Mar 8, 2021 in Web Technology by Rekha (2.2k points)
0 votes
1 answer
asked Mar 8, 2021 in Web Technology by Rekha (2.2k points)
0 votes
1 answer

Browse Categories

...