7.4. Views Generic
View
TemplateView
DetailView
RedirectView
EditView
ListView
FormView
7.4.1. TemplateView
# doctest: +SKIP_FILE
import datetime
from django.views.generic import TemplateView
class TodayView(TemplateView):
template_name = 'templates/index.html'
def get_context_data(self, request, *args, **kwargs):
return {
'today': datetime.date.today(),
'now': datetime.datetime.now(),
}
7.4.2. RedirectView
# doctest: +SKIP_FILE
from django.views.generic import RedirectView
from django.urls import path
urlpatterns = [
path('/index.html', RedirectView.as_view(permanent=False, url='/main')),
]
7.4.3. View
# doctest: +SKIP_FILE
import datetime
from django.http import JsonResponse
from django.views.generic import View
class TodayView(View):
http_method_names = ['get']
def get(self, request, *args, **kwargs):
data = {
'today': datetime.date.today(),
'now': datetime.datetime.now(),
}
return JsonResponse(status=200, data=data, safe=False)