4

Full Disclosure: I'm just learning 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. 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.

# 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:

<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.

4
  • 1
    Please take the effort to indent your code correctly? Commented Dec 7, 2012 at 19:00
  • Poor indentation in Python code is a big no-no ;) Commented Dec 7, 2012 at 19:05
  • Formatted wrong when I posted question, will do it right next time. Commented Dec 7, 2012 at 19:22
  • sorry, my bad. dunno how it went past community review. Commented Dec 7, 2012 at 19:34

1 Answer 1

2

Get the images in your template by calling the model field

{{ post.featured_image }}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.