Django 1.7 |评论表与博客文章

问题描述 投票:-1回答:1

我使用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来生成表单,但不确定在这种情况下该怎么做。

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

python django django-1.7 commenting
1个回答
1
投票

(这个答案可能基于djangoproject.com上的Django教程,也许还有其他地方)

您只需在form.py中定义一个用于注释的表单,在blog_post()函数中创建一个新实例,并在context_dict中包含新创建的表单。然后在模板中,使用{{ thenameyougiveyourforminthecontext }}包含表单的简单版本。

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

这是如何执行此操作的简要概述。请查看我链接的网址,真正了解如何执行此操作。

© www.soinside.com 2019 - 2024. All rights reserved.