[Answered ]-How to pass multiple app's models in one views? [django]

1👍

âś…

You need to related Author and Post models to each other by ForeignKey file, like:

class Post (models.Model):
    author = models.ForeignKey(Author)

Then when you can access to your avatar through post.author.

Updated:

Updating answer after question updated.
I think you need to update your template as follows. use post.post_author.avatar to access the avatar you need:

{% for post in post_list %}
<ul>
   <li>{{ post.title }}</li> # This is works just fine
   <li>{{ post.post_author.avatar }}</li> # This does not work at all
<ul>

{% endfor %}

Considering this please note that you don’t need to search Author’s desperately in your view. So you don’t need to have following line:

context['author'] = Author.objects.all() # This is where I expect the magic is

Because when you have ForeignKey then you already have access to Author through post object.

1👍

Gagik and JF’s answers should have been enough for you to work out what to do. Your foreign key is called “post_author”, so that’s what you should use in the template:

{{ post.post_author.avatar }}

There’s no need for you get all the authors and pass them to the template in your view as you are doing.

Leave a comment