본문 바로가기

Python/Django

[Django] Django - (맛보기)reverse

이번에는 reverse에 대해서 알아보겠습니다. reverse란, path 의 옵션중에 name 을 이용해서 url 을 역으로 찾는 기능입니다. 다르게 표현하면 view 이름을 이용해서(name = view 이름) url을 찾는것입니다.

 

표현이 좀 어설프지만 코드를 보면 명확해 집니다.

 

 

  • /first_app/urls.py - 
from django.urls import path
from . import views

urlpatterns = [
    path('<int:num_page>', views.num_page_view),
    path('<str:topic>/', views.news_view, name='topic-page'),
    path('<int:num1>/<int:num2>', views.add_view),
]

 

  • /first_app/views.py - num_page_view 추가
from django.shortcuts import render
from django.http.response import HttpResponse, Http404, HttpResponseNotFound, HttpResponseRedirect

# Create your views here.
articles = {
    'sports':'Sport page~',
    'finance':'Finance page~',
    'politics':'Politics page~',
    'sports1':'Sport1 page1~',
}    

def news_view(request, topic):
    try:
        result = articles[topic]
        return HttpResponse(result)
    except:
        raise Http404('404 generic ERROR') # 404.html 만들어서 처리 예정임,

# domain.com/first_app/0 --> domain.com/first_app/finance
def num_page_view(request, num_page):
    try:
        print(f'')
        topics_list = list(articles.keys()) # ['sports','finance','politics']
        topic = topics_list[num_page]
        # topic-page 페이지를 기반으로 url 을 찾는함수
        webpage = reverse('topic-page', args=[topic])
        print(f'{topics_list}  {num_page}  {topic}  {webpage}')
        
        # url을 이용해서 redirect 처리함
        return HttpResponseRedirect(webpage)
    except:
        raise Http404('404 generic ERROR') # 404.html 만들어서 처리 예정임,

 

 

  • 결과는 로그로 확인합니다. - HttpResponseRedirect함수의 인자가 "sports" --> "/first_app/sports/" 변경된다.
['sports', 'finance', 'politics', 'sports1']  0  sports  /first_app/sports/
[19/Oct/2022 16:29:41] "GET /first_app/0 HTTP/1.1" 302 0
[19/Oct/2022 16:29:41] "GET /first_app/sports/ HTTP/1.1" 200 11

['sports', 'finance', 'politics', 'sports1']  1  finance  /first_app/finance/
[19/Oct/2022 16:29:54] "GET /first_app/1 HTTP/1.1" 302 0
[19/Oct/2022 16:29:54] "GET /first_app/finance/ HTTP/1.1" 200 13
Not Found: /first_app/2/
[19/Oct/2022 16:29:59] "GET /first_app/2/ HTTP/1.1" 404 2731

['sports', 'finance', 'politics', 'sports1']  2  politics  /first_app/politics/
[19/Oct/2022 16:30:03] "GET /first_app/2 HTTP/1.1" 302 0
[19/Oct/2022 16:30:03] "GET /first_app/politics/ HTTP/1.1" 200 14

['sports', 'finance', 'politics', 'sports1']  3  sports1  /first_app/sports1/
[19/Oct/2022 16:30:07] "GET /first_app/3 HTTP/1.1" 302 0
[19/Oct/2022 16:30:07] "GET /first_app/sports1/ HTTP/1.1" 200 13

Not Found: /first_app/4
[19/Oct/2022 16:30:11] "GET /first_app/4 HTTP/1.1" 404 2538

 

 

결과를 비교하기 위해 이전 방식의 로그를 아래 첨부합니다.

 

['sports', 'finance', 'politics', 'sports1']   sports
[19/Oct/2022 15:07:13] "GET /first_app/0 HTTP/1.1" 302 0 ===> 처음주소
[19/Oct/2022 15:07:13] "GET /first_app/sports/ HTTP/1.1" 200 11 ===> redirect된 주소
1
['sports', 'finance', 'politics', 'sports1']   finance
[19/Oct/2022 15:07:18] "GET /first_app/1 HTTP/1.1" 302 0
[19/Oct/2022 15:07:18] "GET /first_app/finance/ HTTP/1.1" 200 13
2
['sports', 'finance', 'politics', 'sports1']   politics
[19/Oct/2022 15:07:21] "GET /first_app/2 HTTP/1.1" 302 0
[19/Oct/2022 15:07:21] "GET /first_app/politics/ HTTP/1.1" 200 14
3
['sports', 'finance', 'politics', 'sports1']   sports1
[19/Oct/2022 15:07:25] "GET /first_app/3 HTTP/1.1" 302 0
[19/Oct/2022 15:07:25] "GET /first_app/sports1/ HTTP/1.1" 200 13
4
Not Found: /first_app/4
[19/Oct/2022 15:07:28] "GET /first_app/4 HTTP/1.1" 404 2538

 

 

  • 정리(일반적인 방식 --> reverse 방식)