2015-11-02 114 views
-1

我在使用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產生的形式,但不確定在這做什麼場景。

我只是希望觀衆可以在博客文章的頁面上提交他的評論。

回答

1

(這是答案的djangoproject.com可能是基於關閉Django的教程,也許其他地方)

你可以簡單的定義,適用於form.py評論,做它的一個新的實例在blog_post()功能,並在context_dict中包含新鑄造的表格。然後在模板中使用{{ thenameyougiveyourforminthecontext }}包含表單的簡單版本。

然後,在blog_post()方法views.py中,處理表單的提交。 Django網站(特別是page)描述瞭如何處理表單的提交。在表單處理中,您可以使用form.cleaned_data中的字典條目創建一個新的Comment對象(假設您先使用form.is_valid()對其進行驗證)並保存。

這是一個非常如何做到這一點的簡要概述。請看我鏈接的網址,以便真正瞭解如何執行此操作。