-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathviews.py
83 lines (59 loc) · 2.12 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
from django.http import JsonResponse
from django.shortcuts import get_object_or_404
from django.contrib.auth.mixins import LoginRequiredMixin
from django.urls import reverse
from django.views.generic import ListView, DetailView
from blog.models import Article, Author, Tag
def test(request):
return JsonResponse(['wow', {'json': True}], safe=False)
class ArticleListView(ListView):
queryset = Article.objects.filter(published=True)
context_object_name = 'articles'
paginate_by = 4
class ArticleDetailView(DetailView):
queryset = Article.objects.filter(published=True)
context_object_name = 'article'
class DraftListView(LoginRequiredMixin, ListView):
queryset = Article.objects.filter(published=False)
context_object_name = 'drafts'
paginate_by = 8
template_name = 'blog/draft_list.html'
def get_login_url(self):
return reverse('admin:index')
class AuthorListView(ListView):
model = Author
context_object_name= 'authors'
class AuthorDetailView(DetailView):
model = Author
context_object_name = 'author'
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
published_articles = self.object.published_articles()
drafts = self.object.drafts()
context['published_articles'] = published_articles
context['drafts'] = drafts
return context
class AuthorDraftDetailView(LoginRequiredMixin, DetailView):
model = Author
template_name = 'blog/article_detail.html'
context_object_name = 'article'
def get_object(self, **kwargs):
drafts = super().get_object().drafts()
draft_detail = get_object_or_404(drafts, pk=self.kwargs['article_id'])
return draft_detail
def get_login_url(self):
return reverse('admin:index')
class TagListView(ListView):
model = Tag
context_object_name = 'tags'
class TagDetailView(DetailView):
model = Tag
context_object_name = 'tag'
def get_context_data(self, **kwargs):
# Call the base implementation first to get a context
context = super().get_context_data(**kwargs)
published_articles = self.object.published_articles()
print('pa', published_articles)
context['published_articles'] = published_articles
print(context)
return context