Skip to content

Commit 5fb4fc8

Browse files
committed
fix comment post request
1 parent e6f8848 commit 5fb4fc8

File tree

2 files changed

+13
-29
lines changed

2 files changed

+13
-29
lines changed

src/posts/views.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22
from django.contrib.auth.decorators import login_required
33
from django.core.cache import cache
44
from django.db.models import Count
5-
from django.http import (HttpResponse, HttpResponseForbidden,
6-
HttpResponseRedirect, JsonResponse)
5+
from django.http import (HttpResponse, HttpResponseForbidden,JsonResponse)
76
from django.shortcuts import redirect, render
87
from django.urls import reverse
8+
from django.utils.decorators import method_decorator
99
from django.views import View
1010
from pyppeteer import launch
1111
from utils.helpers import paginate
@@ -109,15 +109,15 @@ def toggle_dislike(request, id):
109109

110110
class PostView(View):
111111

112-
@login_required
112+
@method_decorator(login_required)
113113
def post(self, request, id):
114114
comment_form = forms.comment(request.POST)
115115
if comment_form.is_valid():
116116
comment = comment_form.save(commit=False)
117117
comment.post = Post.objects.filter(id=id).first()
118118
comment.user = request.user
119119
comment.save()
120-
return redirect('/post/'+str(id)+'#commentbox')
120+
return redirect('/post/'+str(id)+'#commentBox')
121121

122122
def get(self, request, id):
123123
post = Post.objects.filter(id=id).first()
@@ -134,14 +134,14 @@ def get(self, request, id):
134134
request, 'posts/post.html',
135135
{'title': post.title, 'post': post, 'comments': comments,
136136
'taste': taste})
137-
return HttpResponseRedirect(reverse('home'))
137+
return redirect('home')
138138

139139

140140
def view_pdf(request, id):
141141
post = Post.objects.filter(id=id).first()
142142
if post:
143143
return render(request, 'posts/pdf.html', {'post': post})
144-
return HttpResponseRedirect(reverse('home'))
144+
return reverse('home')
145145

146146

147147
async def download_pdf(request, id):

src/templates/posts/post.html

Lines changed: 7 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -261,8 +261,7 @@
261261
</div>
262262

263263
<div class="col-12">
264-
{% if user.is_authenticated %}
265-
<form method="POST" class="form form--comment">
264+
<form method="POST" class="form form--comment" id="commentBox">
266265
{% csrf_token %}
267266
<div class="row">
268267
<div class="col-12">
@@ -272,33 +271,15 @@ <h4 class="form__title">Add a comment</h4>
272271
<textarea name="text" cols="40" rows="10" class="form__textarea" placeholder="Write your comment"
273272
required></textarea>
274273
</div>
275-
276274
<div class="col-12">
275+
{% if user.is_authenticated %}
277276
<button class="form__btn" type="submit">Comment</button>
278-
</div>
279-
</div>
280-
</form>
281-
{% else %}
282-
<form class="form form--comment">
283-
<div class="row">
284-
<div class="col-12">
285-
<h4 class="form__title">Add a comment</h4>
286-
</div>
287-
<div class="col-12">
288-
{{comment_form.text}}
289-
</div>
290-
291-
<div class="col-12">
277+
{% else %}
292278
<button class="form__btn" type="button" onclick="alert()">Comment</button>
279+
{% endif %}
293280
</div>
294281
</div>
295282
</form>
296-
<script>
297-
function alert() {
298-
Swal.fire("please login!", "You should be a user to comment!", "error");
299-
}
300-
</script>
301-
{% endif %}
302283
</div>
303284
</div>
304285
</div>
@@ -359,6 +340,9 @@ <h2 class="section__title2">Similar news</h2>
359340
</script>
360341
{% endif %}
361342
<script>
343+
function alert() {
344+
Swal.fire("please login!", "You should be a user to comment!", "error");
345+
}
362346
function isUnicode(str) {
363347
var letters = [];
364348
for (var i = 0; i <= str.length; i++) {

0 commit comments

Comments
 (0)