Mohamed Samir
Mohamed Samir

Reputation: 399

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

Upvotes: 12

Views: 9896

Answers (4)

suodeth-byte
suodeth-byte

Reputation: 31

here's an example

app/views

#imports
def indexView(request):
    ....
    return render(request, 'index.html', context)

def loginView(request):
    ....
    return redirect('index')

def articleDetailView(request, id):
    ....
    return redirect(reverse('article-comments', kwargs={'id':id})

def articleCommentsView(request, id):
    ....
    return render(request, 'comment_list.html', context)

proj/urls

#imports
urlpatterns = [
....,
path('', include(app.urls))
]

app/urls

#imports
urlpatterns = [
....,
path('index/', index, name='index'),
path('login/', loginView, name='login'),
path('article/<int:id>/detail', articleDetailView, name='article-detail'),
path('article/<int:id>/comments/',articleCommentsView,  name='article-comments')
....,
]
      

For loginView redirect will return url as-is i.e. 'index' which will be appended to base(project) urlpatterns. Here redirect(reverse('index')) will also work since kwargs is None by default for reverse function and 'index' view doesn't require any kwarg. It returns '/index/' which is passed to redirect(which again will be appended to base urls).

One thing to note is that reverse is used to make complete url - needed for redirect - that is shown in 'articleDetailview'.

Upvotes: 3

Shorya Sharma
Shorya Sharma

Reputation: 457

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.

Upvotes: 2

GwynBleidD
GwynBleidD

Reputation: 20539

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.

Upvotes: 25

Daniel Roseman
Daniel Roseman

Reputation: 599610

GwynBleidD has given you the answer, but there is a reason why you might be getting confused. The Django redirect shortcut accepts arguments in several different forms. One of them is a URLpattern mane, with arguments, that is then passed to reverse to generate the actual URL to redirect to. But that's just a shortcut, to enable a common pattern.

Upvotes: 5

Related Questions