Search code examples
pythondjangoformspostcomments

Django, problem with the comment form below the post


I just can't figure it out for a very long time, I made a conclusion of comments to posts from the admin panel. But I just can't figure out how to make a comment form right under the post for users to comment. Thanks to all!

models.py

class Post(models.Model):
    photo       = models.ImageField(upload_to='media/photos/',null=True, blank=True)
    name_barber = models.CharField(max_length=30)
    description = models.TextField(blank=True, null=True)

    def __str__(self):
        return self.description[:10]


class Comment(models.Model):
    post = models.ForeignKey(Post, related_name='comments', on_delete=models.CASCADE)
    name = models.CharField(max_length=30)
    body = models.TextField(null=True)
    add_date = models.DateTimeField(auto_now_add=True)
    enter code here

    def __str__(self):
        return '%s - %s' % (self.post, self.name)

form.py

class CommentForm(ModelForm):
    class Meta:
        model = Comment
        fields = ('name', 'body')

views.py

class HomePage(ListView):
    model = Post
    template_name = 'main/index.html' 
    context_object_name = 'posts1'

class BarbersPage(ListView):
    model = Post
    template_name = 'main/barbers.html' 
    context_object_name = 'posts'

urls.py

urlpatterns = [
    path('',views.HomePage.as_view(),name='index'),
    path('barbers/',views.BarbersPage.as_view(), name='barbers'),
  
]

barbers.html

{% for post in posts %}
          <img src="{{MEDIA_URL}}{{post.photo.url}}" width="800"  />

          <h3>
        {{ post.name_barber}} 
          </h3>
     <p>{{ post.description}}</p>



     <h3> Comments.. </h3>

    {% if not post.comments.all %}
    no comments yet...<a href = "#">Add one</a>

    {% else %}

        {% for comment in post.comments.all %}

    <strong>
        {{ comment.name }}
        {{ comment.add_date }}
    </strong>
        <p>{{comment.body }}</p>
<br>
        {% endfor %}
         
    {% endif %}           
        
{% endfor %}

Solution

  • You will need to add a form block below the post for users to add a comment.

    Something like this:

    {% for post in posts %}
         <img src="{{MEDIA_URL}}{{post.photo.url}}" width="800"  />
              <h3>
            {{ post.name_barber}} 
              </h3>
         <p>{{ post.description}}</p>
    
         <h3> Comments.. </h3>
    
        {% if not post.comments.all %}
            no comments yet...<a href = "#">Add one</a>
        {% else %}
            {% for comment in post.comments.all %}
                <strong>
                    {{ comment.name }}
                    {{ comment.add_date }}
                </strong>
                <p>{{comment.body }}</p>
                <br>
            {% endfor %}         
        {% endif %}
        Add Comment:<br/>
        <form method="post" action="/AddComment">
            <input type="hidden" id="postid" value="{{post.id}}"/>
            <input type="text" id="newcomment" size="50"/><input type="submit" value="Submit"/>
        </form><br/>
    {% endfor %}
    

    You will need to decide where to submit the comment data and update the action attribute in the form.