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
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