我正在创建一个博客作为学习如何使用django的练习。我创建了博客列表,并创建了博客详细信息页面的链接。在博客详细信息以及应该在页面上的blog_Content
,post_date
,blog_Title
信息中,我还尝试呈现附加到特定博客的comments
由编者/用户
问题:附加到特定作者/用户的评论是 没有被渲染。
Here is the repository if you care to take a look
以下是所有代码:
models.py
from django.db import models
from datetime import date
from django.urls import reverse
from django.contrib.auth.models import User
class Writer(models.Model):
user = models.ForeignKey(User, on_delete = models.SET_NULL, null = True)
writer_description = models.TextField(max_length = 400, help_text = "Enter your description here.")
def get_absolute_url(self):
return reverse('blog:writer-detail', args = [str(self.id)])
def __str__(self):
return self.user.username
class Blog(models.Model):
blog_Title = models.CharField(max_length = 200)
writer = models.ForeignKey(Writer, on_delete=models.SET_NULL, null=True)
blog_Content = models.TextField(max_length = 2000, help_text="Create content here.")
post_date = models.DateField(default = date.today)
class Meta:
ordering = ['-post_date']
def get_absolute_url(self):
return reverse('blog:blog-detail', args = [str(self.id)])
def __str__(self):
return self.blog_Title
class Comment(models.Model):
writer = models.ForeignKey(User, on_delete=models.SET_NULL, null=True)
comment = models.TextField(max_length=1000, help_text="Create comment here.")
post_date = models.DateTimeField(auto_now_add=True)
class Meta:
ordering = ['-post_date']
def __str__(self):
length = 75
if len(self.comment) > length:
Title_string = self.comment[:length] + '...'
else:
Title_string = self.comment
return Title_string
blog_detail.html
{% extends "core_template.html" %}
{% block head %}
<title>Content Stories</title>
{% endblock %}
{% block content %}
<li class="list-group-item">
<p class="display-4">{{blog.blog_Title}}</p>
<hr>
<p class = "text-muted ">Creater: <a href="">{{ blog.writer}}</a></p>
<hr>
<p class="h4 text-muted"> <small> Created On: {{ blog.post_date }}</small></p>
</li>
<li class = "list-group-item" style = "margin-top: 30px; ">
<p class = "mb-0 display-5" style="white-space:pre-wrap;"><small>{{blog.blog_Content}}</small></p>
</li>
这是要呈现注释但未执行此操作的位置。
<p class="display-4">Comments</p> {% for comment in blog.comment_set.all %} <hr> <p>{{ comment.writer}} - {{comment.comment}}</p> {% endfor %} </div>
{% endblock %}