how to add Comment form inside a post detailed

Is there any way I can add a comment form inside a post’s details? I have a view that shows a model object, and I want to allow users to comment on that view. I have tried to use this method, but using that method, a user should leave a post’s details and add their comment somewhere else rather than doing it inside a post’s details? I have try to do that using the method below, but it gives me an error every time I clicked on submit button: TypeError at /video-play/so-da-shakuwa/ Field 'id' expected a number but got <Video: videos/DJ_AB_-_Masoyiya_Official_Video_9UfStsn.mp4>.

views:

def play_video(request, slug):
    play = get_object_or_404(Video, slug=slug)

    if request.method == 'POST':
        comments = request.POST['comments']
        new_comment = Comment.objects.create(
            comments=comments, post_id=play
        )
        new_comment.user = request.user
        new_comment.save()
        return redirect('Videos')
    context = {
        'play':play
    }
    return render(request, 'video/play_video.html', context)

template:

<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/css/bootstrap.min.css"
    rel="stylesheet" integrity="sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC"
    crossorigin="anonymous">
    
   <div class="container">
        <div class="row">
            <div class="col-md-9">
                <form action="" method="post">
                    {% csrf_token %}
                    <div class="input-group">
                        <textarea name="comments" id="comment" cols="10" 
                        class="form-control" placeholder="write your comment"></textarea>
                        <button class="btn btn-primary" type="submit">submit</button>
                    </div>
                </form>
            </div>
        </div>
    </div>

models:

class Video(models.Model):
    user = models.ForeignKey(settings.AUTH_USER_MODEL,  on_delete=models.CASCADE)
    title = models.CharField(max_length=70)
    video = models.FileField(upload_to='videos')
    created_on = models.DateTimeField(auto_now_add=True)
    banner = models.ImageField(upload_to='banner')
    slug = models.SlugField(max_length=100, unique=True)

class Comment(models.Model):
    user = models.ForeignKey(settings.AUTH_USER_MODEL,  on_delete=models.CASCADE)
    comments = models.TextField(max_length=200)
    post = models.ForeignKey(Video, on_delete=models.CASCADE)

    def __str__(self):
        return self.user

>Solution :

You should pass post=play, or post_id=play.pk:

def play_video(request, slug):
    play = get_object_or_404(Video, slug=slug)
    if request.method == 'POST':
        comments = request.POST['comments']
        new_comment = Comment.objects.create(
            comments=comments, post=play, user=request.user
        )
        return redirect('Videos')
    context = {'play': play}
    return render(request, 'video/play_video.html', context)

Note: It is better to use a Form [Django-doc]
than to perform manual validation and cleaning of the data. A Form will not
only simplify rendering a form in HTML, but it also makes it more convenient
to validate the input, and clean the data to a more convenient type.


Note: The __str__ method should return a string, it thus can not return the value of a ForeignKey, since that is not a string, but a model object.

Leave a Reply