[英]Django 1.7 | Comment Form With Blog Post
我使用 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 }}
来包含表单的简单版本。
然后,在views.py
的blog_post()
方法中,处理表单的提交。 Django 网站(特别是这个页面)描述了如何处理表单的提交。 在表格处理,你可以创建一个新的Comment
与字典条目对象form.cleaned_data
(假设你已经使用验证它form.is_valid()
第一个),并将其保存。
这是有关如何执行此操作的非常简短的概述。 请查看我链接的网址以真正了解如何执行此操作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.