Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

redirect vs reverse django

I have experienced using reverse within get_absolute_url method in the model, but I wish I have an idea about the difference between reverse and redirect, I have tried to search on google about it but there is almost nothing I don't know what should I write also to convince stack overflow that I don't have any other description

like image 545
Mohamed Samir Avatar asked Mar 03 '19 01:03

Mohamed Samir


People also ask

What is the difference between redirect and reverse in Django?

The most basic difference between the two is : Redirect Method will redirect you to a specific route in General. Reverse Method will return the complete URL to that route as a String.

What does reverse () do in Django?

the reverse function allows to retrieve url details from url's.py file through the name value provided there. This is the major use of reverse function in Django. The redirect variable is the variable here which will have the reversed value. So the reversed url value will be placed here.

What is Django redirect?

Django comes with an optional redirects application. It lets you store redirects in a database and handles the redirecting for you. It uses the HTTP response status code 301 Moved Permanently by default.

What is reverse and reverse lazy in Django?

Reverse_lazy is, as the name implies, a lazy implementation of the reverse URL resolver. Unlike the traditional reverse function, reverse_lazy won't execute until the value is needed. It is useful because it prevent Reverse Not Found exceptions when working with URLs that may not be immediately known.


Video Answer


1 Answers

Reverse and redirect have a different meaning. Here is a simple explanation:

reverse in Django is used to find the URL of a given resource. Let's say that you have a blog website and from the main page, you want to provide links to your blog posts. You can of course just hard-code /posts/123/ and just change the ID of your blog post in URL, but that makes it hard to change your URL for the post in the future. That's why Django comes with reverse function. All you need to do is to pass the name of your URL path (defined in your urlpatterns) and Django will find for you the correct URL. It is called reverse because it is a reverse process of determining which view should be called for a given URL (which process is called resolving).

Redirects are not specific to Django or any other web frameworks. Redirect means that for a given URL (or action), the user should be instructed to visit a specific URL. This can be done by sending a special redirect request and from there the browser will handle it for the user, so no user action is required in that process. You can use reverse in redirect process to determine the URL that the user should be redirected to.

like image 123
GwynBleidD Avatar answered Oct 04 '22 18:10

GwynBleidD