Comment Form Related issue

5 views
Skip to first unread message

Soumen Khatua

unread,
Apr 26, 2019, 6:34:43 AM4/26/19
to django...@googlegroups.com
Hi Folks,
I want to show one textarea where only logged in users can comment otherwise if they are click the textareabox redirect them to login page just like youutbe comnet box. If anyone have any source code related to this please share.

Thank You.


Regards,
Soumen

Robin Riis

unread,
Apr 26, 2019, 7:07:46 AM4/26/19
to django...@googlegroups.com
class Comment(models.Model):
message = models.TextField()
video = models.ForeignKey(Video, related_name='comments', on_delete=models.CASCADE)
created_at = models.DateTimeField(auto_now_add=True)
created_by = models.ForeignKey(get_user_model(), related_name='comments', on_delete=models.CASCADE)

class CommentForm(forms.ModelForm):
class Meta:
model = Comment
fields = ['message',]

def CommentVideo(request, pk):
video = get_object_or_404(Video, pk=pk)
if request.method == 'POST':
form = CommentForm(request.POST)
if form.is_valid():
comment = form.save(commit=False)
comment.video = video
comment.created_by = request.user
comment.save()
return reverse_lazy('my_cool_place')
else:
form = CommentForm()
return render(request, 'path/to/html/file.html', {'video': video, 'form': form})

something like that?

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAPUw6WadmH6vfXBrpSfNdGrMRgVoBAV-DZfGbJkoO%2B1tVuWyfw%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Soumen Khatua

unread,
Apr 26, 2019, 7:33:48 AM4/26/19
to django...@googlegroups.com
yes,I'm new to django so let me check once.
Thank You for your response.

Robin Riis

unread,
Apr 26, 2019, 8:01:40 AM4/26/19
to django...@googlegroups.com
class Comment(models.Model): <--- in models.py

class CommentForm(forms.ModelForm): <--- in forms.py

def CommentVideo(request, pk): <--- in views.py

and the video is another model i used since you said like youtube. :)

Soumen Khatua

unread,
Apr 26, 2019, 8:05:29 AM4/26/19
to django...@googlegroups.com
Okay,Could you tell me If any user want to update their comment it later the process is same or different?

Thank You


Robin Riis

unread,
Apr 26, 2019, 8:46:42 AM4/26/19
to django...@googlegroups.com
its pretty much the same, you can check on class based view where you have CreateView and UpdateView and so on

and i would recomend to add the following to models.py:
updated_at = models.DateTimeField(null=True)
updated_by = models.ForeignKey(get_user_model(), on_delete=models.CASCADE, null=True)

cheers mate

Reply all
Reply to author
Forward
0 new messages