-
Notifications
You must be signed in to change notification settings - Fork 0
/
views.py
83 lines (69 loc) · 2.51 KB
/
views.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
from django.shortcuts import render,get_object_or_404,redirect
from django.utils import timezone
from blog.models import Post,Comment
from blog.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=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
fields = '__all__'
class PostUpdateView(LoginRequiredMixin,UpdateView):
login_url='/login/'
redirect_field_name='blog/post_detail.html'
from_class=PostForm
model=Post
fields = '__all__'
class PostDeleteView(LoginRequiredMixin,DeleteView):
model=Post
success_url=reverse_lazy('post_list')
fields = '__all__'
class DraftListView(LoginRequiredMixin,ListView):
login_url='/login/'
redirect_field_name='blog/post_list.html'
model=Post
fields = '__all__'
def get_queryset(self):
return Post.objects.filter(published_date__isnull=True).order_by('created_date')
@login_required
def post_publish(request,pk):
post=get_object_or_404(Post,pk=pk)
post.publish()
return redirect('post_detail',pk=pk)
@login_required
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.post=post
comment.save()
return redirect('post_detail',pk=post.pk)
else:
form=CommentForm()
return render(request,'blog/comment_form.html',{'form':form})
@login_required
def comment_approve(request,pk):
comment=get_object_or_404(Comment,pk=pk)
comment.approve()
return redirect('post_detail',pk=comment.post.pk)
@login_required
def comment_remove(request,pk):
comment=get_object_or_404(Comment,pk=pk)
post_pk=comment.post.pk
comment.delete()
return redirect('post_detail',pk=post_pk)