Skip to content

Commit cbff40f

Browse files
author
Mohammed Ayaz
committed
Add class based views, individual post pages, and link to post on home page
1 parent 6fc2d48 commit cbff40f

File tree

5 files changed

+19
-4
lines changed

5 files changed

+19
-4
lines changed

blog/templates/blog/home.html

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,13 @@
22
{% block content %}
33
{% for post in posts%}
44
<article class="media content-section">
5+
<img class="rounded-circle article-img" src="{{ post.author.profile.image.url }}">
56
<div class="media-body">
67
<div class="article-metadata">
78
<a class="mr-2" href="#">{{ post.author }}</a>
89
<small class="text-muted">{{ post.date_posted | date:"F d, Y" }}</small>
910
</div>
10-
<h2><a class="article-title" href="#">{{ post.title }}</a></h2>
11+
<h2><a class="article-title" href="{% url 'post-detail' post.id %}">{{ post.title }}</a></h2>
1112
<p class="article-content">{{ post.content }}</p>
1213
</div>
1314
</article>

blog/urls.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
from django.urls import path
2+
from .views import PostListView, PostDetailView
23
from . import views
34

45
urlpatterns = [
5-
path('', views.home, name='blog-home'),
6+
path('', PostListView.as_view(), name='blog-home'),
7+
path('post/<int:pk>/', PostDetailView.as_view(), name='post-detail'),
68
path('about/', views.about, name='blog-about'),
79

8-
]
10+
]

blog/views.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from django.shortcuts import render
22
from django.http import HttpResponse
3+
from django.views.generic import ListView, DetailView
34
from .models import Post
45

56

@@ -9,5 +10,15 @@ def home(request):
910
}
1011
return render(request, 'blog/home.html', context)
1112

13+
class PostListView(ListView):
14+
model = Post
15+
template_name = 'blog/home.html' # <app>/<model>_<viewtype>.html
16+
context_object_name = 'posts'
17+
ordering = ['-date_posted']
18+
19+
class PostDetailView(DetailView):
20+
model = Post
21+
22+
1223
def about(request):
13-
return render(request, 'blog/about.html', {'title': 'About'})
24+
return render(request, 'blog/about.html', {'title': 'About'})

db.sqlite3

0 Bytes
Binary file not shown.

manage.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,4 @@ def main():
1919

2020
if __name__ == '__main__':
2121
main()
22+

0 commit comments

Comments
 (0)