[英]How to add new object attribute with Python/Django
Full Disclosure: I'm just learning Django 全面披露:我只是在学习Django
The homepage of our site is displaying recent blog posts. 我们网站的主页显示了最近的博客文章。 In views.py
there is a class that defines the attributes that can be called using post.get_attribute
to call the recent posts on the homepage. 在views.py
有一个类定义了可以使用post.get_attribute
调用主页上最近发布的属性的属性。 I am trying to add a new attribute that is in the model featured_image
but am not able to get it to render on the homepage. 我正在尝试在模型featured_image
添加一个新属性,但无法使其呈现在主页上。
# From models.py
featured_image = models.ImageField(_('featured image'),
upload_to='images/posts',
blank=True, null=True)
# Added featured_image to class in views.py
def get_posts(request, post_type):
if post_type == 'personal':
posts = list(Post.personal_objects.all().values('title', 'slug',
'author__username',
'views',
'featured_image'))[:8]
elif post_type == 'business':
posts = list(Post.business_objects.all().values('title', 'slug',
'author__username',
'views',
'featured_image'))[:8]
else:
raise Http404
return HttpResponse(json.dumps(posts), mimetype='application/json')
How I am trying to call the featured image in home.html
: 我如何尝试在home.html
调用精选图片:
<a href="{{ post.get_absolute_url }}">
<img src="{{ post.get_featured_image_url }}" />
</a>
What am I missing to get these images pulling in? 我缺少什么来获取这些图像? Really appreciate any insight. 真的很感激任何见识。 For broader context visit this link. 有关更广泛的上下文,请访问此链接。
通过调用模型字段来获取模板中的图像
{{ post.featured_image }}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.