-1

I working on a blog using Django 1.7.

I want to include a comment form with each blog post but unsure how to do that.

Here is my code:

Models:

class Category(models.Model):
    name = models.CharField(max_length=128, unique=True)
    slug = models.SlugField(unique=True)

    def save(self, *args, **kwargs):
            self.slug = slugify(self.name)
            super(Category, self).save(*args, **kwargs)

    def __unicode__(self):
            return self.name

class Post(models.Model):
    title = models.CharField(max_length=100, unique=True)
    slug = models.SlugField(max_length=100, unique=True)
    body = models.TextField()
    posted = models.DateTimeField(db_index=True, auto_now_add=True)
    category = models.ForeignKey(Category)

    def save(self, *args, **kwargs):
        self.slug = slugify(self.title)
        super(Post, self).save(*args, **kwargs)

    def __unicode__(self):
        return '%s' % self.title

class Comment (models.Model):
    comment_body = models.TextField()
    commented_on = models.DateTimeField(db_index=True,auto_now=True)
    commenter = models.ForeignKey(User)
    post = models.ForeignKey(Post)

    def __unicode__(self):
        return '%s' % self.comment_body

class Tag (models.Model):
    title = models.CharField(max_length=100, unique=True)
    slug = models.SlugField(max_length=100, unique=True)
    post = models.ManyToManyField(Post)

    def save(self, *args, **kwargs):
        self.slug = slugify(self.title)
        super(Tag, self).save(*args, **kwargs)

    def __unicode__(self):
        return '%s' % self.title

Views:

def blog_post(request, blog_post_slug):
    post_detail = Post.objects.get(slug=blog_post_slug)
    comments = Comment.objects.filter(post=post_detail.id)
    tags = Tag.objects.filter(post=post_detail.id)
    context_dict = {'post': post_detail, 'slug': blog_post_slug, 'comments':comments, 'tags':tags}
    response = render(request,'blog/blog_post.html', context_dict)
    return response

Template:

{% extends 'base.html' %}

{% load staticfiles %}

{% block title %}{{ post.title }}{% endblock %}

{% block body_block %}
<div class="container">
    <div class="post-preview">
    {% if post %}
    <div>
        <h2>{{ post.title }}</h2>
        <small>{{ post.category }}</small>
        <p class="post-meta">{{ post.posted }}</p>
        <p>{{ post.body }}</p>
<h3>Comments</h3>
        {% if comments %}
        {% for comment in comments %}
       <h5> {{ comment.commenter | capfirst }}</h5>
          <p>  {{ comment.comment_body }}</p>
            <small>{{ comment.commented_on }}</small>
        <hr>
        {% endfor %}
        {% else %}
        <small>No comments Yet </small>
        {% endif %}

        <h3>Tags</h3>
        {% if tags %}
        {% for tag in tags %}
       <span> <a href="{% url 'blog:tag' tag.slug %}"> {{ tag.title }}</a>,</span>
        {% endfor %}
        {% else %}
        <small>Without Tags! </small>
        {% endif %}

    </div>
    {% else %}
    <strong>No such post</strong>
    {% endif %}
        </div>
    </div>
{% endblock %}

I am using form.py to generate forms but unsure what to do in this scenario.

I just want that viewer can submit his comments on the page of blog post.

jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
Muhammad Ahmed
  • 428
  • 2
  • 6
  • 17

1 Answers1

1

(This is answer was probably based off the Django tutorials at djangoproject.com and maybe elsewhere)

You can simply define a form for commenting in form.py, make a new instance of it in your blog_post() function, and include the newly minted form in context_dict. Then in the template, use {{ thenameyougiveyourforminthecontext }} to include a simple version of the form.

Then, in the blog_post() method in views.py, handle the form's submission. The Django website (specifically this page) describes how to handle the form's submission. In the form handling, you can create a new Comment object with the dictionary entries in form.cleaned_data (assuming you have validated it using form.is_valid() first) and save it.

This is a very brief overview of how to do this. Please look at the url I linked to truly understand how to do this.

ben
  • 344
  • 2
  • 10