Skip to content

Commit c687fe5

Browse files
committed
Now the articles' page could display its comments (view-only, cannot-edit)
1 parent aa245a3 commit c687fe5

File tree

3 files changed

+45
-7
lines changed

3 files changed

+45
-7
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# Generated by Django 2.1.3 on 2018-12-04 12:34
2+
3+
from django.db import migrations, models
4+
import django.db.models.deletion
5+
6+
7+
class Migration(migrations.Migration):
8+
9+
dependencies = [
10+
('articles', '0003_comment'),
11+
]
12+
13+
operations = [
14+
migrations.AlterField(
15+
model_name='comment',
16+
name='article',
17+
field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='comments', to='articles.Article'),
18+
),
19+
]

chapter08/articles/models.py

+12-2
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,19 @@ def get_absolute_url(self):
1818

1919

2020
class Comment(models.Model):
21-
article = models.ForeignKey(Article, on_delete=models.CASCADE)
21+
22+
article = models.ForeignKey(
23+
Article,
24+
on_delete=models.CASCADE,
25+
related_name="comments",
26+
)
27+
2228
comment = models.CharField(max_length=140)
23-
author = models.ForeignKey(get_user_model(), on_delete=models.CASCADE)
29+
30+
author = models.ForeignKey(
31+
get_user_model(),
32+
on_delete=models.CASCADE
33+
)
2434

2535
def __str__(self):
2636
return self.comment

chapter08/templates/article_list.html

+14-5
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,24 @@
1313
<span class="text-muted">by {{ article.author }} | {{ article.date |date:"M d Y" }}</span>
1414
</div>
1515

16-
<div class="card-body">
17-
{{ article.body }}
18-
</div>
19-
20-
<div class="card-footer text-center text-muted">
16+
<div class="card-body">
17+
<p>{{ article.body }}</p>
2118
<a href="{% url 'article_edit' article.pk %}">Edit</a> |
2219
<a href="{% url 'article_delete' article.pk %}">Delete</a>
2320
</div>
2421

22+
<div class="card-footer">
23+
{% for comment in article.comments.all %}
24+
<p>
25+
<span class="font-weight-bold">
26+
{{ comment.author }} &middot;
27+
</span>
28+
29+
{{ comment }}
30+
</p>
31+
{% endfor %}
32+
</div>
33+
2534
</div>
2635

2736
<br/>

0 commit comments

Comments
 (0)