Hello! This is Ponta, a Shiba Inu. The other day, when I thought there was a Shiba Inu looking at me all the time, it was a mirror. Good grief. ..
Yesterday's topic: After doing "GET Request and Query Parameter Volume", I thought about it, for example, the URL http://example.com/Ponta/20200825/ (http://example.com/ (name) / (date 8 digits) /) When I receive the request in the format of, I thought that I could read Ponta's diary on August 25, 2020, so I will try it. It's a difficult problem for dogs.
Set the urlpatterns in urls.py to get the value from the url.
(venv_dog) Ponta@shiba_app # cat wan/urls.py
from django.urls import path
from . import views
urlpatterns = [
path('<dogname>/<diarydate>/', views.index, name='index'),
]
(venv_dog) Ponta@shiba_app #
You can now receive the name in the variable dogname and the date in the variable diarydate.
Next, write views.py as follows:
from django.http import HttpResponse
def index(request, dogname, diarydate):
year = diarydate[:4]
month = diarydate[4:6]
date = diarydate[6:]
res = "<h1>" + dogname + "s diary</h1>"
res = res + "<p>" + year + "Year"+ month + "Month" + date + "Day</p>"
return HttpResponse(res)
From the diary date, the values of (4 digits per year) (2 digits per month) (2 digits per day), respectively year = diarydate[:4], month = diarydate[4:6], date = diarydate[6:] I am getting it at.
Let's display it.
Oh! It seems to have worked. By the way, the following display was displayed on the console running the test server.
[25/Aug/2020 13:02:49] "GET /wan/Ponta/20200825/ HTTP/1.1" 200 47
See you later! Bye bye!
Recommended Posts