简体   繁体   English

Django 1.7 | 带有博客帖子的评论表

[英]Django 1.7 | Comment Form With Blog Post

I working on a blog using Django 1.7.我使用 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.我正在使用 form.py 生成表单,但不确定在这种情况下该怎么做。

I just want that viewer can submit his comments on the page of blog post.我只是希望观众可以在博客文章的页面上提交他的评论。

(This is answer was probably based off the Django tutorials at djangoproject.com and maybe elsewhere) (这个答案可能基于 djangoproject.com 和其他地方的 Django 教程)

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 .您可以简单地在 form.py 中定义一个用于评论的表单,在您的blog_post()函数中创建它的新实例,并将新创建的表单包含在context_dict Then in the template, use {{ thenameyougiveyourforminthecontext }} to include a simple version of the form.然后在模板中,使用{{ thenameyougiveyourforminthecontext }}来包含表单的简单版本。

Then, in the blog_post() method in views.py , handle the form's submission.然后,在views.pyblog_post()方法中,处理表单的提交。 The Django website (specifically this page ) describes how to handle the form's submission. Django 网站(特别是这个页面)描述了如何处理表单的提交。 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.在表格处理,你可以创建一个新的Comment与字典条目对象form.cleaned_data (假设你已经使用验证它form.is_valid()第一个),并将其保存。

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.请查看我链接的网址以真正了解如何执行此操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM