from django.shortcuts import render, get_object_or_404, redirect
from django.utils import timezone
#from django.utils.timezone.now`
from blog.models import Post, Comment
from .forms import PostForm,CommentForm
from django.urls import reverse_lazy
from django.contrib.auth.decorators import login_required
from django.contrib.auth.mixins import LoginRequiredMixin
from django.views.generic import (TemplateView, ListView, DetailView, CreateView,
UpdateView, DeleteView)
# Create your views here.
class Aboutview(TemplateView):
template_name = 'about.html'
class PostListView(ListView):
model = Post
def get_queryset(self):
return Post.objects.filter(published_date_lte=timezone.now()).order_by('-published_date')
class PostDetailView(DetailView):
model = Post
class CreatePostView(LoginRequiredMixin,CreateView):
Login_url = '/Login/'
redirect_field_name = 'blog/post_detail.html'
from_class = PostForm
model = Post
class PostUpdateView(LoginRequiredMixin, UpdateView):
Login_url = '/Login/'
redirect_field_name = 'blog/post_detail.html'
from_class = PostForm
model = Post
#class PostDeleteView(loginrequiredMixin, DeleteView):
class PostDeleteView(LoginRequiredMixin,DeleteView):
model = Post
success_url = reverse_lazy('post_list')
class DraftListView(LoginRequiredMixin, ListView):
login_url = '/Login/'
redirect_field_name = 'blog/post_list.html'
model = Post
def get__queryset(self):
return Post.objects.filter(published_date_isnull=True).order_by('created_date')
#############################
##############################
def add_comment_to_post(request, pk):
post = get_object_or_404(Post, pk=pk)
if request.method == "POST":
form = CommentForm(request.POST)
if form.is_valid():
comment = form.save(commit=False)
comment.save()
return redirect(
'post_detail', pk=
post.pk)
else:
form = CommentForm()
return render(request, 'blog/comment_form.html', {'form': form})
@login_required
def post_publish(request, pk):
post = get_object_or_404(Post, pk=pk)
post.publish
return redirect('blog/post_detail.html', pk=pk)
#@login_required
def comment_approve(request,pk):
comment = get_object_or_404(comment,pk=pk)
comment.approve()
#@login_required
def comment_remove(request,pk):
#comment = get_object_or_404(comment, pk=pk)
#comment.remove()
comment = get_object_or_404(comment, pk=pk)
comment_delete()
blog urls:
#from django.conf.urls import path
from django.urls import include, path
from blog import views
urlpatterns = [
path('',views.PostListView.as_view(), name='Post_list'),
path('about', views.Aboutview.as_view, name='about'),
path('Post/(?P<pk>\d+)', views.PostDetailView.as_view(), name='post_detail'),
path('Post/new/', views.CreatePostView.as_view(), name='post_new'),
path('Post/(?P<pk>\d+)/edit/', views.PostUpdateView.as_view(), name='post_edit'),
path('Post/(?P<pk>\d+)/remove/', views.PostDeleteView.as_view(), name='post_remove'),
path('drafts/', views.DraftListView.as_view(), name='post_draft_list'),
#path('Post/(?P<pk>\d+)/comment/',views.add_comment_to_post()error, name='add_comment_to_post'),
path('Post/(?P<pk>\d+)/comment/',views.add_comment_to_post, name='add_comment_to_post'),
path('comment/(?P<pk>\d+)/approve/',views.comment_approve,name='comment_approve'),
path('comment/(?P<pk>\d+)/remove/',views.comment_remove,name='comment_remove'),
path('Post/(?P<pk>\d+)/publish/',views.post_publish, name='post_publish'),
]
site url:
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('blog.urls')),
path('accounts/login/',auth_views.LoginView.as_view(template_name='blog/login.html')),
path('accounts/logout/',views.logout, name='logout',kwargs={'next_page': '/'}),
]
How to reslove this field error: