from django.views.generic import TemplateView, ListView, DetailView, CreateView, UpdateView, DeleteView
class LandingPageView(TemplateView):
template_name = 'landing.html'
# Optional: Change context data dict
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context['title'] = 'Landing Page'
return context
class PostsListView(ListView):
queryset = Post.objects.all()
# Optional
# context_object_name = "posts" (default: post_list)
# template_name = 'posts.html' (default: posts/post_list.html)
class PostsDetailView(DetailView):
model = Post # object var in template
# Optional
# template_name = 'post.html' (default: posts/post_detail.html)
class PostsCreateView(CreateView):
form_class = PostForm
template_name = 'posts/post_create.html' # no default value
def get_success_url(self):
return reverse('posts-list')
# Optional: Overwrite form data (before save)
def form_valid(self, form):
if self.request.user.is_authenticated:
from.instance.author = self.request.user
return super().form_valid(form)
class PostsUpdateView(UpdateView):
model = Post
form_class = PostForm
template_name = 'posts/post_update.html'
def get_success_url(self):
return reverse('post-list')
# Optional: Change context data dict
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context['submit_text'] = 'Update'
return context
class PostsDeleteView(DeleteView):
model = Post
template_name = 'posts/post_delete.html'
success_url = reverse_lazy('posts-list')
# Urls.py route declaration
path('<int:pk>/update/', PostsUpdateView.as_view(), name='post-update')