FE PARADISE

[Django] reverse 본문

백엔드/Python

[Django] reverse

PARADISE247 2026. 2. 8. 21:40
반응형

reverse

이름만 보면 뭘 반대로 뒤집는 느낌이 들지만 뭔가 배열의 정렬을 반대로 뒤집어 반환한다거나 문자열을 뒤집어주는 역할을 하지 않음.

reversed([1, 2, 3])  # → [3, 2, 1]

그런 역할을 하는 게 Python 문법에 reversed라고 있긴 함. 그건 나중에 알아보자.

?? 그래서 Django reverse가 뭔데

보통 정신병이 나에게 온다

하지만 reverse를 이용하면

내가 정신병에게 간다

 

URL → View

path('hello/', views.hello_world, name='hello_world')

 

View name → URL

# urls.py

urlpatterns = [
    path('hello_world/', HelloWorldView, name='hello_world'),
    path('create/', CreateView.as_view(), name='create'),
]

# views.py
class CreateView(CreateView):
    ...
    success_url = reverse_lazy('exampleapp:hello_world')
success_url = reverse_lazy('accountapp:hello_world')

GET /create/

CreateView 실행 & 결과가 성공적 

'hello_world/'로 리다이렉트 됨

따라서 'hello_world/' 호출 → HelloWorldView 실행



reverse
: 뷰를 가리키는 이름으로 → URL 문자열 찾음

따라서 Django 관점에서 이 방향은 역방향 조회이기 때문에 이름을 reverse로 지은 것

순서를 차근차근 살펴보면

1. URLConf를 샅샅이 뒤진다.

2. 'accountapp:hello_world'를 찾는다.

3. 'hello_world/' 문자열을 생성한다. 

반응형

'백엔드 > Python' 카테고리의 다른 글

[Django] decorator  (0) 2026.02.17
isinstance  (0) 2026.01.25