我是 Django 新手,我在教程后重復一遍,但我無法顯示來自 html 數(shù)據(jù)庫的注釋。urls.pystatic(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)設置 MEDIA_ROOT = os.path.join(BASE_DIR, 'media/')models.pyfrom django.db import modelsclass Post(models.Model): photo = models.ImageField(upload_to='media/photos/',null=True, blank=True) name_barber = models.CharField(max_length=30) description = models.TextField(blank=True, null=True) def __str__(self): return self.description[:10]class Comment(models.Model): post = models.ForeignKey(Post, related_name='comments', on_delete=models.CASCADE) name = models.CharField(max_length=255) body = models.TextField() date_add = models.DateTimeField(auto_now_add=True) def __str__(self): return '%s - %s' % (self.post, self.name)html文件} </style>{% for post in posts %}<img src="{{MEDIA_URL}}{{post.photo.url}}" width="800" /><h3>{{ post.name_barber}}</h3><p>{{ post.description}}</p>{% endfor %}<h3> Comments.. </h3>{% if not post.comments.all %} no comments yet...<a href = "#">Add one</a>{% else %} {% for comment in post.comments.all %} <strong> {{ comment.name }} {{ comment.date_add }} </strong> {{comment.body }} {% endfor %}{% endif %}通過管理面板添加評論后,頁面顯示:還沒有評論.. 有什么問題請告訴我?
1 回答

POPMUISE
TA貢獻1765條經驗 獲得超5個贊
刪除條件并將標簽與 forloop 一起if/else使用。{% empty %}
{% for comment in post.comments.all %}
<strong>
{{ comment.name }}
{{ comment.date_add }}
</strong>
{{comment.body }}
{% empty %}
no comments yet...<a href = "#">Add one</a>
{% endfor %}
添加回答
舉報
0/150
提交
取消