How to add a new attribute to an object using Python / Django

Full disclosure: I'm just learning Django

The homepage of our site displays the latest blog posts. There is a class in views.py that defines attributes that can be called using post.get_attribute to trigger the latest posts on the main page. I am trying to add a new attribute that is in the featured_image model, but I cannot display it on the main page.

 # 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 am I trying to call the displayed image in home.html :

 <a href="{{ post.get_absolute_url }}"> <img src="{{ post.get_featured_image_url }}" /> </a> 

What am I missing to get these images? Really appreciate any insight. For a broader context, visit this link.

+4
source share
1 answer

Get images in the template by calling the model field

 {{ post.featured_image }} 
+2
source

All Articles