Django 1.7 |与博客文章的评论表

问题描述:

我在使用Django 1.7的博客上工作。Django 1.7 |与博客文章的评论表

我想在每个博客文章中加入评论表单,但不确定如何做到这一点。

这里是我的代码:

型号:

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 

浏览:

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 

模板:

{% 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 %} 

我使用form.py产生的形式,但不确定在这做什么场景。

我只是希望观众可以在博客文章的页面上提交他的评论。

(这是答案的djangoproject.com可能是基于关闭Django的教程,也许其他地方)

你可以简单的定义,适用于form.py评论,做它的一个新的实例在blog_post()功能,并在context_dict中包含新铸造的表格。然后在模板中使用{{ thenameyougiveyourforminthecontext }}包含表单的简单版本。

然后,在blog_post()方法views.py中,处理表单的提交。 Django网站(特别是page)描述了如何处理表单的提交。在表单处理中,您可以使用form.cleaned_data中的字典条目创建一个新的Comment对象(假设您先使用form.is_valid()对其进行验证)并保存。

这是一个非常如何做到这一点的简要概述。请看我链接的网址,以便真正了解如何执行此操作。