姜戈 1.7 |博客评论表 Post
Django 1.7 | Comment Form With Blog Post
我正在使用 Django 1.7 开发博客。
我想在每个博客中添加一个评论表单post但不确定如何操作。
这是我的代码:
型号:
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 生成表单,但不确定在这种情况下该怎么做。
我只希望浏览者可以在博客页面上提交评论 post。
(这个答案可能基于 djangoproject.com 和其他地方的 Django 教程)
您可以简单地在 form.py 中定义一个用于评论的表单,在您的 blog_post()
函数中创建它的一个新实例,并将新创建的表单包含在 context_dict
中。然后在模板中,使用 {{ thenameyougiveyourforminthecontext }}
包含表单的简单版本。
然后,在views.py
中的blog_post()
方法中,处理表单的提交。 Django 网站(特别是这个 page)描述了如何处理表单的提交。在表单处理中,您可以使用 form.cleaned_data
中的字典条目创建一个新的 Comment
对象(假设您首先使用 form.is_valid()
对其进行了验证)并保存它。
这是对如何执行此操作的非常的简要概述。请看看我链接的 url 才能真正理解如何做到这一点。
我正在使用 Django 1.7 开发博客。
我想在每个博客中添加一个评论表单post但不确定如何操作。
这是我的代码:
型号:
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 生成表单,但不确定在这种情况下该怎么做。
我只希望浏览者可以在博客页面上提交评论 post。
(这个答案可能基于 djangoproject.com 和其他地方的 Django 教程)
您可以简单地在 form.py 中定义一个用于评论的表单,在您的 blog_post()
函数中创建它的一个新实例,并将新创建的表单包含在 context_dict
中。然后在模板中,使用 {{ thenameyougiveyourforminthecontext }}
包含表单的简单版本。
然后,在views.py
中的blog_post()
方法中,处理表单的提交。 Django 网站(特别是这个 page)描述了如何处理表单的提交。在表单处理中,您可以使用 form.cleaned_data
中的字典条目创建一个新的 Comment
对象(假设您首先使用 form.is_valid()
对其进行了验证)并保存它。
这是对如何执行此操作的非常的简要概述。请看看我链接的 url 才能真正理解如何做到这一点。