10000 Comment posting done! · moktadirul-raju/DjangoBlog@958090f · GitHub
[go: up one dir, main page]

Skip to content

Commit 958090f

Browse files
committed
Comment posting done!
1 parent 0137edb commit 958090f

File tree

8 files changed

+17
-6
lines changed

8 files changed

+17
-6
lines changed

blog/__pycache__/urls.cpython-37.pyc

9 Bytes
Binary file not shown.

blog/__pycache__/views.cpython-37.pyc

102 Bytes
Binary file not shown.

blog/urls.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
path('tag/<str:tag>/', views.archeive_posts_by_tag, name='archeive_tag'),
1616
path('category/<str:category>/', views.archeive_posts_by_category, name='archeive_category'),
1717
path('author/<str:author>/', views.archeive_posts_by_author, name='archeive_author'),
18-
path('posts/<int:pk>/', views.post_details),
18+
path('posts/<int:pk>/', views.post_details, name='post_details'),
1919
path('posts/search_result', views.search_view, name='search_view'),
2020
path('submit_comment', views.submit_comment, name='submit_comment'),
2121

blog/views.py

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -81,15 +81,25 @@ def search_view(request):
8181
return render(request, 'blog/archeive.html', context)
8282

8383
def submit_comment(request):
84-
post = Post.objects.get(id=1)
85-
user = User.objects.get(id=1)
8684

8785
if request.method == 'POST' :
86+
8887
comment_title = request.POST.get('comment_title')
8988
comment_content = request.POST.get('comment_content')
90-
c = Comment(post.id,comment_title, comment_content, user.id, datetime.now())
89+
post_id = request.POST.get('post_id')
90+
current_user = request.user
91+
92+
c = Comment()
93+
c.post = Post.objects.get(id=post_id)
94+
c.title = comment_title
95+
c.content = comment_content
96+
c.author = User.objects.get(id=current_user.id)
97+
c.published_date = datetime.now()
9198
c.save()
9299

100+
return HttpResponseRedirect(reverse('index')) # have to work here
101+
102+
93103
@login_required
94104
def user_logout(request):
95105
logout(request)

db.sqlite3

0 Bytes
Binary file not shown.
63 KB
Loading

templates/blog/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
{% if posts %}
66
{% for post in posts %}
77
<div class="post">
8-
<h2><a href="blog/posts/{{post.id}}/"> {{ post.title}} </a></h2>
8+
<h2><a href="blog/posts/{{post.id}}"> {{ post.title}} </a></h2>
99
<ul>
1010
<li>By <a href="blog/author/{{post.author}}">{{ post.author}}</a></li>
1111
<li><a href="blog/{{post.published_date.year}}/{{post.published_date.month}}/{{post.published_date.day}}"> <i class="fa fa-clock-o" aria-hidden="true"></i> {{ post.published_date }}</a></li>

templates/blog/single.html

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,13 @@ <h2>{{post.title}}</h2>
3232
</div>
3333
<div class="card-body">
3434

35-
<form action="{% url 'blog:submit_comment'%}" method="POST">
35+
<form action="{% url 'blog:submit_comment'%}" method="POST">
3636
{% csrf_token %}
3737

3838
<div class="form-group">
3939

4040
<input type="text" name='comment_title'>
41+
<input type="hidden" name='post_id' value="{{post.id}}">
4142
<textarea class="form-control" id="comment-box" rows="3" name="comment_content"></textarea>
4243

4344
</div>

0 commit comments

Comments
 (0)
0