Created
April 9, 2019 05:16
-
-
Save Xifeng2009/6872ff02d287587532e1909132dfcd62 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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