All kinds of Django View
1. First Example
Suppose that we are going to build a blog application. The following codes will create the index page.
# urls.py
urlpatterns = [
path('', views.index, name='index'),
]
# views.py
def index(request):
latest = Post.objects.order_by("-modified_time")
return render(request, "index.html", {"post_list": latest})
If we use View class, the codes will be easier to read.
# urls.py
urlpatterns = [
path('', views.IndexView.as_view(), name='index'),
]
# views.py
from django.views.generic import ListView
class IndexView(ListView):
queryset = Post.objects.order_by('-modified_time')
template_name = 'index.html'
context_object_name = 'post_list'
model form
https://docs.djangoproject.com/en/4.1/topics/forms/modelforms/