Develop/Django
Django 경로 변환(타입 추가) - 4
김잠봉
2023. 2. 26. 15:19
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를 실행해서 확인하면 각각의 페이지를 확인할 수 있다.
728x90
SMALL