Skip to content

Commit

Permalink
Added class-based views
Browse files Browse the repository at this point in the history
  • Loading branch information
yahyaaly151989 committed Aug 20, 2023
1 parent 8df1683 commit c2689f9
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 13 deletions.
Binary file modified Module02/mysite/blog/__pycache__/urls.cpython-311.pyc
Binary file not shown.
Binary file modified Module02/mysite/blog/__pycache__/views.cpython-311.pyc
Binary file not shown.
2 changes: 1 addition & 1 deletion Module02/mysite/blog/templates/blog/post/list.html
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,5 @@ <h2>
{{ post.body|truncatewords:30|linebreaks }}
<hr />
{% endfor %}
{% include "pagination.html" with page=posts %}
{% include "pagination.html" with page=page_obj %}
{% endblock %}
3 changes: 2 additions & 1 deletion Module02/mysite/blog/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
app_name = 'blog'

urlpatterns = [
path('', views.post_list, name='post_list'),
# path('', views.post_list, name='post_list'),
path('', views.PostListView.as_view(), name='post_list'),
path('<int:year>/<int:month>/<int:day>/<slug:post>/', views.post_detail, name='post_detail'),
]
33 changes: 22 additions & 11 deletions Module02/mysite/blog/views.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,30 @@
from django.shortcuts import render, get_object_or_404
from .models import Post
from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger
from django.views.generic import ListView

def post_list(request):
post_list = Post.objects.all()
paginator = Paginator(post_list, 2)
page_number = request.GET.get('page', 1)

# def post_list(request):
# post_list = Post.objects.all()
# paginator = Paginator(post_list, 2)
# page_number = request.GET.get('page', 1)

try:
posts = paginator.page(page_number)
except PageNotAnInteger:
posts = paginator.page(1)
except EmptyPage:
posts = paginator.page(paginator.num_pages)
return render(request, 'blog/post/list.html', {'posts': posts})
# try:
# posts = paginator.page(page_number)
# except PageNotAnInteger:
# posts = paginator.page(1)
# except EmptyPage:
# posts = paginator.page(paginator.num_pages)
# return render(request, 'blog/post/list.html', {'posts': posts})

class PostListView(ListView):
"""
Alternative post list view
"""
model = Post
context_object_name = 'posts'
paginate_by = 3
template_name = 'blog/post/list.html'

def post_detail(request, year, month, day, post):
post = get_object_or_404(
Expand Down

0 comments on commit c2689f9

Please sign in to comment.