text: from django.shortcuts import render
  
  # Create your views here.
  
  
  from django.shortcuts import render, get_object_or_404, redirect
  from django.contrib.auth.mixins import LoginRequiredMixin, UserPassesTestMixin
  from django.contrib.auth.models import User
  from django.contrib.auth.decorators import login_required
  
  from django.views.generic import (
      ListView,
      DetailView,
      CreateView,
      UpdateView,
      DeleteView,
      FormView
  )
  from .models import Post, Comment
  from users.models import Profile
  from .forms import CommentForm
  
  
  class PostListView(ListView):
      model = Post
      template_name = 'packages/home.html'  # <app>/<model>_<viewtype>.html
      context_object_name = 'posts'
      ordering = ['-date_posted']
      paginate_by = 5
  
  
  
  class UserPostListView(ListView):
  
      template_name = 'packages/user_posts.html'  # <app>/<model>_<viewtype>.html
      context_object_name = 'c_user'
  
  
      def get_queryset(self):
          user = get_object_or_404(User, username=self.kwargs.get('username'))
          return user
  
  class UserDetail(DetailView):
      model = Profile
  
  class PostCreateView(LoginRequiredMixin, CreateView):
      model = Post
      fields = ['title', "level", 'cover_letter']
  
  
      def form_valid(self, form):
          form.instance.author = self.request.user
          return super().form_valid(form)
  
  
  class PostUpdateView(LoginRequiredMixin, UserPassesTestMixin, UpdateView):
      model = Post
      fields = ['title',  "level", 'cover_letter']
  
      def form_valid(self, form):
          form.instance.author = self.request.user
          return super().form_valid(form)
  
      def test_func(self):
          post = self.get_object()
          if self.request.user == post.author:
              return True
          return False
  
  
  class PostDeleteView(LoginRequiredMixin, UserPassesTestMixin, DeleteView):
      model = Post
      success_url = '/'
  
      def test_func(self):
          post = self.get_object()
          if self.request.user == post.author:
              return True
          return False
  
  
  class PostDetailView(LoginRequiredMixin, DetailView):
      model = Post
  
  
  
  
  
  def add_comment_to_post(request, pk):
      post = get_object_or_404(Post, pk=pk)
      author = request.user
      if request.method == "POST":
          form = CommentForm(request.POST)
          if form.is_valid():
              comment = form.save(commit=False)
              comment.post = post
              comment.author = author
              comment.save()
              return redirect('post-detail', pk=post.pk)
      else:
          form = CommentForm()
      return render(request, 'packages/add_comment_to_post.html', {'form': form})
  
  
  @login_required
  def comment_remove(request, pk):
      comment = get_object_or_404(Comment, pk=pk)
      comment.delete()
      return redirect('post-detail', pk=comment.post.pk)