8000 Issue #4 done · moktadirul-raju/DjangoBlog@0df21bb · GitHub
[go: up one dir, main page]

Skip to content

Commit 0df21bb

Browse files
committed
Issue #4 done
1 parent a129e5a commit 0df21bb

9 files changed

+ 8000 68
-8
lines changed
Binary file not shown.

blog/__pycache__/forms.cpython-37.pyc

0 Bytes
Binary file not shown.
0 Bytes
Binary file not shown.

blog/__pycache__/views.cpython-37.pyc

483 Bytes
Binary file not shown.

blog/context_processors.py

+1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from blog.models import Post
22

3+
#Popularing dictionary for all_posts, tags, categories, post years
34
def post_widget(request):
45
all_posts = Post.objects.order_by('published_date')
56
years = []

blog/forms.py

+1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ def check_for_s(value):
2424

2525

2626

27+
# This class will be workable in the next version
2728
class PostForm(forms.ModelForm):
2829
title = forms.CharField(validators=[check_for_s])
2930
content = forms.CharField(widget=forms.Textarea)

blog/models.py

+1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ def __str__(self):
1414
return self.user.username
1515

1616

17+
1718
class Post(models.Model):
1819
title = models.CharField(max_length=250,unique=True)
1920
content = models.CharField(max_length=1000)

blog/views.py

+48-7
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
# Create your views here.
1616

17+
#Index page with latest posts
1718
def index(request):
1819
post_list = Post.objects.order_by('published_date')
1920

@@ -24,47 +25,83 @@ def index(request):
2425
my_dict = {'posts':posts}
2526
return render(request, 'blog/index.html', context=my_dict)
2627

27-
28+
#Post details page
2829
def post_details(request, pk):
2930
post_list = Post.objects.get(id=pk)
3031
post_id = post_list.id
3132
comments = post_list.comments.all()
3233
my_dict = {'post':post_list, 'comments':comments}
3334
return render(request, 'blog/single.html', context=my_dict)
3435

36+
#All posts by published year
3537
def archeive_posts(request, year):
3638
post_list = Post.objects.filter(published_date__year = year)
37-
context = {'year':year,'posts':post_list}
39+
paginator = Paginator(post_list, 5) # Show 5 posts per page
40+
page = request.GET.get('page')
41+
posts = paginator.get_page(page)
42+
43+
context = {'year':year,'posts':posts}
3844
return render(request, 'blog/archeive.html', context)
3945

46+
#All posts by post tag
4047
def archeive_posts_by_tag(request, tag):
4148
post_list = Post.objects.filter(tag=tag)
42-
context = {'posts':post_list}
49+
50+
paginator = Paginator(post_list, 5) # Show 5 posts per page
51+
page = request.GET.get('page')
52+
posts = paginator.get_page(page)
53+
54+
context = {'posts':posts}
4355
return render(request, 'blog/archeive.html', context)
4456

57+
#All posts by post category
4558
def archeive_posts_by_category(request, category):
4659
post_list = Post.objects.filter(category=category)
47-
context = {'posts':post_list}
60+
61+
paginator = Paginator(post_list, 5) # Show 5 posts per page
62+
page = request.GET.get('page')
63+
posts = paginator.get_page(page)
64+
65+
context = {'posts':posts}
4866
return render(request, 'blog/archeive.html', context)
4967

68+
#All posts by a specific author
5069
def archeive_posts_by_author(request, username):
5170
author = User.objects.get(username=username)
5271
post_list = Post.objects.filter(author=author.id)
53-
context = {'posts':post_list}
72+
73+
paginator = Paginator(post_list, 5) # Show 5 posts per page
74+
page = request.GET.get('page')
75+
posts = paginator.get_page(page)
76+
77+
context = {'posts':posts}
5478
return render(request, 'blog/archeive.html', context)
5579

80+
#All posts by published_date
5681
def archeive_posts_by_date(request, year, month, day):
5782
post_list = Post.objects.filter(published_date__year=year, published_date__month=month, published_date__day=day)
58-
context = {'posts':post_list}
83+
84+
paginator = Paginator(post_list, 5) # Show 5 posts per page
85+
page = request.GET.get('page')
86+
posts = paginator.get_page(page)
87+
88+
context = {'posts':posts}
5989
return render(request, 'blog/archeive.html', context)
6090

91+
#Search Result
6192
def search_view(request):
6293
if request.method == 'GET' :
6394
search_query = request.GET.get('search_box')
6495
post_list = Post.objects.filter(title__contains=search_query ) | Post.objects.filter(content__contains=search_query ) | Post.objects.filter(tag__contains=search_query ) | Post.objects.filter(category__contains=search_query )
65-
context = {'posts':post_list}
96+
97+
paginator = Paginator(post_list, 5) # Show 5 posts per page
98+
page = request.GET.get('page')
99+
posts = paginator.get_page(page)
100+
101+
context = {'posts':posts}
66102
return render(request, 'blog/archeive.html', context)
67103

104+
#Comment submission by logged in user
68105
@login_required
69106
def submit_comment(request):
70107

@@ -88,11 +125,13 @@ def submit_comment(request):
88125
return HttpResponseRedirect(reverse('index')) # have to work here
89126

90127

128+
#Log out from b
91129
@login_required
92130
def user_logout(request):
93131
logout(request)
94132
return HttpResponseRedirect(reverse('index'))
95133

134+
#Authentication and Login
96135
def user_login(request):
97136

98137
if request.method == 'POST':
@@ -115,6 +154,7 @@ def user_login(request):
115154
return render(request,'blog/login.html', {})
116155

117156

157+
#New user registration
118158
def registration(request):
119159
registered = False
120160

@@ -148,6 +188,7 @@ def registration(request):
148188
'profile_form':profile_form,
149189
'registered':registered})
150190

191+
#New Post creation in custom backend.this function is not workable right now.It will updated in next version
151192
@login_required
152193
def post_form_view(request):
153194
form = forms.PostForm()

templates/blog/archeive.html

+17-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,23 @@ <h2><a href="/blog/posts/{{post.id}}"> {{ post.title}} </a></h2>
3535
<li class="page-item"><a class="page-link" href="#">Next</a></li>
3636
</ul>
3737
</nav> -->
38-
38+
<div class="pagination">
39+
<span class="step-links">
40+
{% if posts.has_previous %}
41+
<a href="?page=1">&laquo; first</a>
42+
<a href="?page={{ posts.previous_page_number }}">previous</a>
43+
{% endif %}
44+
45+
<span class="current">
46+
Page {{ posts.number }} of {{ posts.paginator.num_pages }}.
47+
</span>
48+
49+
{% if posts.has_next %}
50+
<a href="?page={{ posts.next_page_number }}">next</a>
51+
<a href="?page={{ posts.paginator.num_pages }}">last &raquo;</a>
52+
{% endif %}
53+
</span>
54+
</div>
3955
{% else %}
4056
<p>Sorry!! No post has been found!!!</p>
4157
{% endif %}

0 commit comments

Comments
 (0)
0