forked from realpython/materials
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathviews.py
More file actions
37 lines (28 loc) · 1.08 KB
/
Copy pathviews.py
File metadata and controls
37 lines (28 loc) · 1.08 KB
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
from django.shortcuts import render
from blog.forms import CommentForm
from blog.models import Post, Comment
def blog_index(request):
posts = Post.objects.all().order_by("-created_on")
context = {"posts": posts}
return render(request, "blog_index.html", context)
def blog_category(request, category):
posts = Post.objects.filter(categories__name__contains=category).order_by(
"-created_on"
)
context = {"category": category, "posts": posts}
return render(request, "blog_category.html", context)
def blog_detail(request, pk):
post = Post.objects.get(pk=pk)
comments = Comment.objects.filter(post=post)
form = CommentForm()
if request.method == "POST":
form = CommentForm(request.POST)
if form.is_valid():
comment = Comment(
author=form.cleaned_data["author"],
body=form.cleaned_data["body"],
post=post,
)
comment.save()
context = {"post": post, "comments": comments, "form": form}
return render(request, "blog_detail.html", context)