[PYTHON] What is a dog? Django--Get Name and Date from URL Volume

Get the value from the URL address

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.

urlpatterns settings

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.

Description of views.py

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.

スクリーンショット 2020-08-25 22.06.25.png

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

What is a dog? Django--Get Name and Date from URL Volume
What is a dog? Django installation volume
What is a dog? Python installation volume
What is a dog? Challenge Django templates! Volume
What is a dog? Django--Create a custom user model
What is a dog? Django--Create a custom user model 2
What is a distribution?
What is a dog? Django App Creation Start Volume--startapp
What is a terminal?
What is a dog? Django--Volume of using values obtained from URLs in class-based views
What is a hacker?
What is a dog? Django App Creation Start Volume--startproject
What is a pointer?
What is a dog? Django--Getting Started with Form for the First Time POST Transmission Volume
What is a decision tree?
[Python] Python and security-① What is Python?
What is a super user?
What is a system call
[Definition] What is a framework?
What is a callback function?
What is a python map?
[Python] What is a zip function?
[Python] What is a with statement?
What is a lexical scope / dynamic scope?
Gender is determined from the name.
[Introduction to Python] What is the difference between a list and a tuple?
Migration from Python2 to Python3 (Python2 is rebuilt as a virtual environment and coexists)
How to get a namespaced view name from a URL (path_info) in Django