我已经阅读了所有相关主题,并且精疲力尽了我的研究,但我似乎无济于事。我将包含相关代码:
models.py
class Blog(models.Model):
title = models.CharField(max_length=100, unique=True)
slug = models.SlugField(max_length=100, unique=True)
posted = models.DateTimeField(db_index=True, auto_now_add=True)
body = models.TextField(blank=True)
category = models.ForeignKey('myapp.Category')
images = models.ForeignKey('myapp.Image')
def __unicode__(self):
return self.title
@permalink
def get_absolute_url(self):
return ('teensite.myapp.views.view_post', None, {'slug': self.slug})
class Image(models.Model):
def content_file_name(instance, filename):
return os.path.join(MEDIA_ROOT, filename)
title = models.CharField(max_length=60, blank=True, null=True)
number = models.IntegerField()
image = models.FileField(upload_to=content_file_name)
body = models.TextField(blank=True)
citation = models.URLField(blank=True)
created = models.DateTimeField(auto_now_add=True)
def save(self, *args, **kwargs):
"""Save image dimensions."""
super(Image, self).save(*args, **kwargs)
im = PImage.open(os.path.join(MEDIA_ROOT, self.image.name))
self.width, self.height = im.size
super(Image, self).save(*args, ** kwargs)
def __unicode__(self):
return self.image.name
def size(self):
"""Image size."""
return "%s x %s" % (self.width, self.height)
def get_absolute_url(self):
return ('http://www.teensdigest.com/media/'+self.image.name)
views.py
def home(request):
return render(request, 'home.html', {'cats': Category.objects.all(),
'blogs' : reversed(Blog.objects.all()),
})
我需要home.html模板才能在blog.images中显示第一个图像。问题在于{{pic.get_absolute_url}}的其余模板均按预期方式呈现,但是{{pic.get_absolute_url}}没有返回值。我尝试了其他方法来访问Image模型中的其他值,但它们都无法像{{blog.images.all.0.title}}等那样工作。
慕勒3428872
白板的微信
相关分类