I have a simple Django application which I would appreciate a fresh pair of eyes to help me fix this problem.
我有一个简单的Django应用程序,我很欣赏一双新的眼睛来帮助我解决这个问题。
I have been trying to add the comments functionality from the djangogirls tutorial to my app called 'timeline'.
我一直在尝试将djangogirls教程中的评论功能添加到我的应用程序“timeline”中。
So far I have added a comment model to my models.py:
到目前为止,我已经为我的models.py添加了一个评论模型:
class Comment(models.Model):
post = models.ForeignKey('timeline.Photo', related_name='comments')
author = models.ForeignKey(settings.AUTH_USER_MODEL, default=1)
text = models.TextField()
created_date = models.DateTimeField(default=timezone.now)
def __unicode__(self):
return self.text
def __str__(self):
return self.text
which already contains a photo model:
已包含照片模型:
class Photo(models.Model):
user = models.ForeignKey(settings.AUTH_USER_MODEL, default=1)
title = models.CharField(max_length=120)
slug = models.SlugField(unique=True)
image = ProcessedImageField(upload_to=upload_location,
null=True,
blank=False,
processors=[Transpose(), ResizeToFit(1000, 1000, False)],
#processors=[Transpose(), ResizeToFit(width=960)],
format='JPEG',
options={'quality': 50},
width_field="width_field",
height_field="height_field")
height_field = models.IntegerField(default=0)
width_field = models.IntegerField(default=0)
description = models.TextField()
updated = models.DateTimeField(auto_now=True, auto_now_add=False)
timestamp = models.DateTimeField(auto_now=False, auto_now_add=True)
def __unicode__(self):
return self.title
def __str__(self):
return self.title
def get_absolute_url(self):
return reverse("timeline:detail", kwargs={"slug": self.slug})
class Meta:
ordering = ["-timestamp", "-updated"]
I am struggling to make the comments appear within my html but so far i have followed the instructions and used:
我正在努力使评论出现在我的HTML中,但到目前为止我已按照说明使用:
{% for comment in post.comments.all %}
<div class="comment">
<div class="date">{{ comment.created_date }}</div>
<strong>{{ comment.author }}</strong>
<p>{{ comment.text|linebreaks }}</p>
</div>
{% empty %}
<p>No comments here yet :(</p>
{% endfor %}
view:
def photo_detail(request, slug=None):
if not request.user.is_authenticated():
return HttpResponseRedirect("/accounts/login")
instance = get_object_or_404(Photo, slug=slug)
share_string = quote_plus(instance.description)
context = {
"title": instance.title,
"instance": instance,
"share_string": share_string,
}
return render(request, "photo_detail.html", context)
I have added the comment model to my admin and I am able to add comments ok but they are simply not displaying and it is driving me nuts. I am sure a fresh pair of eyes will spot this easily. Using Django 1.9 and if any more code required let me know!
我已将评论模型添加到我的管理员,我可以添加评论,但它们只是没有显示,这让我疯狂。我相信一双新鲜的眼睛很容易发现这一点。使用Django 1.9,如果需要更多代码,请告诉我!
code on Github
Github上的代码
Any help is much appreciated folks! Cheers.
任何帮助都非常感谢大家!干杯。
1 个解决方案
#1
0
You are not passing any object called post
into the template for rendering.
您没有将任何名为post的对象传递到模板中进行渲染。
You need to change either the context passed from the view:
您需要更改从视图传递的上下文:
context = {
"title": instance.title,
"photo": instance,
"share_string": share_string,
}
Or change the code in the template:
或者更改模板中的代码:
{% for comment in instance.comments.all %}
(one or the other, not both)
(一个或另一个,不是两个)
I would suggest to change the context in the view to use the more meaningful name photo
.
我建议更改视图中的上下文以使用更有意义的名称照片。
#1
0
You are not passing any object called post
into the template for rendering.
您没有将任何名为post的对象传递到模板中进行渲染。
You need to change either the context passed from the view:
您需要更改从视图传递的上下文:
context = {
"title": instance.title,
"photo": instance,
"share_string": share_string,
}
Or change the code in the template:
或者更改模板中的代码:
{% for comment in instance.comments.all %}
(one or the other, not both)
(一个或另一个,不是两个)
I would suggest to change the context in the view to use the more meaningful name photo
.
我建议更改视图中的上下文以使用更有意义的名称照片。