我正在尝试使用 django 实现博客应用程序。在应用程序的主页上,旁边会有帖子列表,我需要在那里显示个人资料图片?



我正在尝试使用 django 实现博客应用程序。在应用程序的主页上,旁边会有帖子列表,我需要在那里显示个人资料图片。我不知道该怎么接近?

 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 %}

我需要显示与其帖子对应的用户的个人资料图片。我试过了,但我没有得到。

您应该传递外键以调用用户的配置文件模型

{% for img in images %}
   <img src='{{ img.author.profile.image.url }}' >
{% endfor %}

相关内容

最新更新