diff --git a/socialapp/forms.py b/socialapp/forms.py index 569dabb..b7e5bb5 100644 --- a/socialapp/forms.py +++ b/socialapp/forms.py @@ -5,3 +5,8 @@ class UserPostForm(Form): text = CharField(widget=Textarea( attrs={'cols': 100, 'rows': 5}), label="Enter your post here") + +class CommentPostForm(Form): + text = CharField(widget=Textarea( + attrs={'cols': 100, 'rows': 5}), + label="Enter your comment here") \ No newline at end of file diff --git a/socialapp/templates/post_details.html b/socialapp/templates/post_details.html index b92fe13..ddddd9b 100644 --- a/socialapp/templates/post_details.html +++ b/socialapp/templates/post_details.html @@ -1,9 +1,27 @@ {% extends 'layout.html' %} {% block content %} + Go back to first page

Post details

{{ post.author }}

{{ post.date_added }}

{{ post.text }}

+ +
+ {% csrf_token %} + {{ form.as_p }} + +
+ + {% for comment in comments %} +
+
+

{{ comment.author }}

+

{{ comment.date_added }}

+

+ {{ comment.text}} +

+
+ {% endfor %} {% endblock %} diff --git a/socialapp/views.py b/socialapp/views.py index c941e88..1f04ad4 100644 --- a/socialapp/views.py +++ b/socialapp/views.py @@ -1,7 +1,8 @@ from django.shortcuts import render, redirect +from django.http import HttpResponse -from socialapp.models import UserPost -from socialapp.forms import UserPostForm +from socialapp.models import UserPost, UserPostComment +from socialapp.forms import UserPostForm, CommentPostForm def index(request): @@ -23,6 +24,27 @@ def index(request): def post_details(request, pk): + post = UserPost.objects.get(pk=pk) - context = {'post': post} - return render(request, 'post_details.html', context) + if request.method == 'GET': + form = CommentPostForm() + comments=UserPostComment.objects.filter(post=post).order_by('-date_added') + + context = { + 'post': post, + 'form':form, + 'comments':comments, + } + + #return HttpResponse(UserPostComment.objects.get(post=post)) # + return render(request, 'post_details.html', context) + + elif request.method == 'POST': + form = CommentPostForm(request.POST) + + + if form.is_valid(): + text = form.cleaned_data['text'] + user_comment = UserPostComment(text=text, post=post) + user_comment.save() + return redirect('/post/'+str(post.pk))