-1

I'm trying to implement blog app with django.In homepage of app there will be list of posts alongside i need show there profile picture.I don't know how to approach?

 models.py

 from django.db import models
 from django.conf import settings
 from django.contrib.auth.models import User

 # Create your models here.
 class Post(models.Model):
     title=models.CharField(max_length=100)
     desc=models.TextField()
     date=models.DateField(auto_now=True)
     author=models.ForeignKey(settings.AUTH_USER_MODEL, 
                             to_field="username",on_delete=models.CASCADE)

 class Profile(models.Model):
     user = models.OneToOneField(User,on_delete=models.CASCADE)
     image = models.ImageField(default='default.jpg',upload_to='pics')

 <!----Post------>
 {% for post in posts %}
    <div class="list">
        <div class="con">
            <div class='logo2'>
                {% for img in img %}
                    <img src='' >
                {% endfor %}
            </div>
            <h3 style="color:DodgerBlue;">{{post.author}}</h3>
            <h6 style="font-family: montserrat,sans-serif;"> {{post.date}} 
                                                                   </h6>
            <div class="line"></div>
            <a href="{% url 'post_detail' pk=post.pk %}"><h1><b> 
                                          {{post.title}}</b></h1></a>
            <div style="font-size: 17px;">
                <p>{{post.desc}}</p>
            </div>
        </div>
    </div>
    {% endfor %}

I need to show profile picture of user corresponding to thier post.I tried but i didn't got.

  • 1
    duplicate question I think, check the links below : [Add image/avatar field to users in django](https://stackoverflow.com/questions/6396442/add-image-avatar-field-to-users-in-django) [Django user profile](https://stackoverflow.com/questions/6085025/django-user-profile) [Extending the User model with custom fields in Django](https://stackoverflow.com/questions/44109/extending-the-user-model-with-custom-fields-in-django) [Django user profile tutorial_ youtube](https://www.youtube.com/watch?v=FdVuKt_iuSI) [Django user profile tutorial _ youtube](https://www.youtube.com/watch?v=FdVuKt_iuSI) – Ali Akhtari Jun 23 '19 at 16:39

1 Answers1

0

You should pass the foreign key to call profile model for a user

{% for img in images %}
   <img src='{{ img.author.profile.image.url }}' >
{% endfor %}
Akash D
  • 780
  • 2
  • 11
  • 27