我在使用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產生的形式,但不確定在這做什麼場景。
我只是希望觀衆可以在博客文章的頁面上提交他的評論。