Skip to content

Commit

Permalink
With submit form
Browse files Browse the repository at this point in the history
  • Loading branch information
rensequitin committed Feb 20, 2018
1 parent c5507bc commit c93dd35
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 3 deletions.
14 changes: 12 additions & 2 deletions forum/post/templates/post/index.html
Original file line number Diff line number Diff line change
@@ -1,8 +1,18 @@
{% if all_post %}
<ul>
{% for post in all_post %}
<li>{{ post.title }} - {{post.author.name}}</li>
<li>Comments{{ post.comment_set.all }}</li>
<h2>"{{ post.title }}" - {{post.author.name}}</h2>
<h4 style="line-height:1px;"> Comments </h4>
{% for comment in post.comment_set.all %}
<li style="margin-left:15px;">{{ comment.comment }}</li>
{% endfor %}

<form action="{% url 'post:favorite' post.id%}" method="POST">
{% csrf_token %}
<input type="textarea" autocomplete="off" name="reply">
<input type="submit" value="Add comment" name="">
</form>

{% endfor %}
</ul>
{% else %}
Expand Down
3 changes: 2 additions & 1 deletion forum/post/urls.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
from django.urls import path

from . import views

app_name = "post"
urlpatterns = [
path('',views.index, name ='index'),
path('<post_id>/favorite/',views.favorite, name ='favorite'),
]
7 changes: 7 additions & 0 deletions forum/post/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,10 @@ def showAllPosts(request):
all_post = Post.objects.all
context = { 'all_post' : all_post, }
return HttpResponse(template.render(context, request))

def favorite(request, post_id):
all_post = Post.objects.all
post = Post.objects.get(pk=post_id)
add_comment = post.comment_set.create(comment=request.POST['reply'])
add_comment.save()
return render(request,'post/index.html', {'all_post':all_post})

0 comments on commit c93dd35

Please sign in to comment.