Abdelhamed Abdin
Abdelhamed Abdin

Reputation: 594

what is the difference between reverse, reverse_lazy, resolve and redirect? [Django]

what is the difference between those next terms: reverse, reverse_lazy, resolve and redirect I think when I have to use return then I have to use redirect but if this is true?; why I can't use reverse instead of redirect? also, when I should use these methods?

Upvotes: 2

Views: 1672

Answers (1)

C O D E
C O D E

Reputation: 85

resolve() function can be used for resolving URL paths to the corresponding view functions.

reverse() function: It’s similar to the url template tag which use to convert namespaced url to real url pattern.

For example:

def test_list_reverse():
    """cheeses:list should reverse to /cheeses/."""
    assert reverse('cheeses:list') == '/cheeses/'

def test_list_resolve():
    """/cheeses/ should resolve to cheeses:list."""
    assert resolve('/cheeses/').view_name == 'cheeses:list'

• Reversing the view name should give us the absolute URL. • Resolving the absolute URL should give us the view name.

The example in resolve gives a good idea of what's it for. Generally I've only used reverse as I need the url. I haven't used it but resolve gives you the view that the reverse url points to

For more details read this article

Upvotes: 4

Related Questions