개발로 자기계발
article thumbnail
728x90

경로에 타입을 지정할 수 있다.

 

1) challenges/url.py의 경로에 타입을 추가해 본다.

from django.urls import path

from . import views


urlpatterns = [
    path("<int:month>", views.monthly_challenge_by_number),
    path("<str:month>", views.monthly_challenge)
]

 

 

2) View에서는 int로 받는 값을 그대로 return 한다.

from django.shortcuts import render
from django.http import HttpResponse, HttpResponseNotFound


def monthly_challenge_by_number(request, month):
    return HttpResponse(month)

def monthly_challenge(request, month):
    if month == "january":
        response = "Django is fun!"
    elif month == "february":
        response = "Coding for at least 1 hour every day!"
    elif month == "march":
        response = "Hard Coding is hard"
    else:
        return HttpResponseNotFound("Not month")
    return HttpResponse(response)

 

3) Django를 실행해서 확인하면 각각의 페이지를 확인할 수 있다.

 

http://127.0.0.1:8000/challenges/1                                   <=>                       http://127.0.0.1:8000/challenges/february

 

728x90
SMALL
profile

개발로 자기계발

@김잠봉

틀린부분이나 조언이 있다면 언제든 환영입니다:-)