Skip to content

Instantly share code, notes, and snippets.

@Xifeng2009
Created April 9, 2019 05:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Xifeng2009/6872ff02d287587532e1909132dfcd62 to your computer and use it in GitHub Desktop.
Save Xifeng2009/6872ff02d287587532e1909132dfcd62 to your computer and use it in GitHub Desktop.
# In models.py
class Comment(models.Model):
topic = models.ForeignKey(Topic)
comment = models.CharField(max_length=200, null=True, blank=True, verbose_name='评论')
date_added = models.DateTimeField(auto_now_add=True, verbose_name='创建时间')
class Meta:
verbose_name_plural = 'comments'
def __str__(self):
return self.comment[:10]
# In admin.py
admin.site.register(Comment)
# In views.py
def topic(request, topic_id):
topic = Topic.objects.get(id=topic_id)
comments = topic.comment_set.order_by('-date_added')
context = {
'topic': topic,
'comments': comments
}
return render(request, 'examples/topic.html', context)
# In templates
<p>Comments: </p>
{% for comment in comments %}
<li>{{ comment.comment }}</li>
{% endfor %}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment