What is a dog? Django--Volume of using values obtained from URLs in class-based views

Use class-based view

This is Ponta, a Shiba Inu. I went to Lawson with my owner. Dogs can't enter the store, so when I was waiting outside, they bought fried chicken. The owner seems to be accumulating points on the Ponta card. Even though the names are the same, the Ponta card is a raccoon character.

Now, let's display it in class-based view today. The biggest question is how to pass the value obtained from the URL to the class-based view.

urls.py For the time being, urls.py looks like this.

wan/urls.py


from django.urls import path
from . import views

app_name = 'wan'

urlpatterns = [
    path('<dogname>/<diarydate>/', views.WanView.as_view(), name='index'),
]

I'm going to define WanView in views.py, so I'm using "views.WanView.as_view ()". as_view () is a function that meets Django's view criteria. [^ 1]

views.py The main views.py.

Enter the template name in template_name. Overridden get_context_data to get the value from the URL. [^ 2] You can get the value with self.kwargs.get.

from django.views import generic

class WanView(generic.TemplateView):
    
    template_name = "wan/index.html"

    def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)
        diarydate = self.kwargs.get('diarydate')
        dogname = self.kwargs.get('dogname')

        context['year'] = diarydate[:4]
        context['month'] = diarydate[4:6]
        context['date'] = diarydate[6:]
        
        context['title'] = dogname + "s diary"
        context['description'] = "Today's rice is yakiniku!"

        return context

Display test

スクリーンショット 2020-08-26 7.01.00.png

Same as yesterday.

See you dear! Bye bye!

[^ 1]: What is Django's class-based view as_view? [^ 2]: TemplateView patterns you'll want to learn first with Django

Recommended Posts

What is a dog? Django--Volume of using values obtained from URLs in class-based views
What is a dog? POST Sending Volume Using Django--forms.py
What is a dog? Volume of GET request and query parameters
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
Extract elements (using a list of indexes) in a NumPy style from a Python list / tuple
What is a dog? Django--Create a custom user model
Load images from URLs using Pillow in Python 3
What is a dog? Django--Create a custom user model 2
Record YouTube views in a spreadsheet using Lambda
This is a sample of function application in dataframe.
What is a dog? Django App Creation Start Volume--startproject
Basics of Python learning ~ What is a string literal? ~
Display a histogram of image brightness values in python
What is a recommend engine? Summary of the types
To receive multiple return values ​​from a function executed using parallel processing (Pool) in Python
How to create an instance of a particular class from dict using __new__ () in python
Started Python: Swap an array of values obtained from SQL results to a list type and use it in IN of another query