Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions socialapp/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
18 changes: 18 additions & 0 deletions socialapp/templates/post_details.html
Original file line number Diff line number Diff line change
@@ -1,9 +1,27 @@
{% extends 'layout.html' %}
{% block content %}
<a href="{% url 'index' %}">Go back to first page</a>
<h2>Post details</h2>
<div class="user-post">
<p>{{ post.author }}</p>
<p>{{ post.date_added }}</p>
<p>{{ post.text }}</p>
</div>

<form method="post">
{% csrf_token %}
{{ form.as_p }}
<input type="submit" value="Post Comment">
</form>

{% for comment in comments %}
<div class="user-comment">
<hr>
<p>{{ comment.author }}</p>
<p>{{ comment.date_added }}</p>
<p>
{{ comment.text}}
</p>
</div>
{% endfor %}
{% endblock %}
30 changes: 26 additions & 4 deletions socialapp/views.py
Original file line number Diff line number Diff line change
@@ -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):
Expand All @@ -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))