【问题标题】:How to add profile_pic field from another model in one model如何在一个模型中添加另一个模型的 profile_pic 字段
【发布时间】:2021-10-10 03:27:26
【问题描述】:

我想在我的个人资料模型中的博客视图页面中添加 profile_pic

models.py

class Profile(models.Model):
    user = models.OneToOneField(User,null=True, on_delete=models.CASCADE)
    bio = models.TextField()
    profile_pic = models.ImageField(null=True, blank=True, upload_to="images/profile/")

class StudentBlogModel(models.Model):
    title = models.CharField(max_length=255)
    author = models.ForeignKey(User, on_delete=models.CASCADE)
    body = RichTextField(blank=True,null=True)
    snippet = models.TextField(max_length=255)

Views.py

class BlogView(ListView):
    model = StudentBlogModel
    template_name = 'blog/home.html'
    context_object_name = "StudentBlogModel_list"   
    paginate_by = 10  

home.html

<div class="container">
        <div class="row">
            <div class="col-lg-8">
            <hr>
                <div class="profile-feed">
                    <div class="d-flex align-items-start profile-feed-item">
                      <img src="{{ }}" alt="profile" class="img-sm rounded-circle mb-5 mx-2">
                      <div class="ml-4">
                        <h6>
                        {% if StudentBlogModel.author.first_name or StudentBlogModel.author.last_name%}
                            <small class="card-title">{{StudentBlogModel.author.first_name|capfirst}}
                            {{StudentBlogModel.author.last_name|capfirst}} </small>
                        {% else %}
                            <small class="card-title">{{StudentBlogModel.author.username|capfirst}}</small>
                        {% endif %}
                       <small class="ml-4 text-muted"><i class="mdi mdi-clock mr-1"></i> {{StudentBlogModel.post_date}}</small>
                        </h6>
                        <p>
                          {{StudentBlogModel.title}}
                        </p>

在 src 中写什么来获取特定作者的个人资料图片

【问题讨论】:

    标签: html django django-models


    【解决方案1】:

    试试这个:

    <img src="{{ StudentBlogModel.author.profile_pic.url  }}" alt="profile" class="img-sm rounded-circle mb-5 mx-2">
    

    顺便说一句,如果您要创建用户个人资料,我建议您改用 DetailView:

    class BlogView(DetailView):
        model = StudentBlogModel
        template_name = 'blog/home.html'
        context_object_name = "StudentBlogModel_list"   
        paginate_by = 10  
    

    【讨论】:

      【解决方案2】:

      尝试在您的视图中传递 pk

      def profile(request, pk):
          uprofile = User.objects.get(id=pk)
          return render(request, 'accounts/users_profile.html', {'uprofile':uprofile})
      

      当您想查看其他人的个人资料时,这是我的用户个人资料代码,也许会有所帮助

      把它放在你的 urls.py 中

      path('profile/<int:pk>/', views.profile, name='profile'),
      

      【讨论】:

        【解决方案3】:
        <img src="{{ StudentBlogModel.author.profile_pic.url  }}" alt="profile" class="img-sm rounded-circle mb-5 mx-2">
        

        如果这不起作用,您是否已向作者添加了个人资料图片或为个人资料图片创建了默认图片。

        class Profile(models.Model):
            user = models.OneToOneField(User,null=True, on_delete=models.CASCADE)
            bio = models.TextField()
            profile_pic = models.ImageField(null=True, blank=True, upload_to="images/profile/", default='default.jpg')
        

        在媒体文件夹中添加 default.jpg 并且应该可以工作。

        【讨论】:

          【解决方案4】:
          {% if StudentBlogModel.author.profile.profile_pic %}
                <img src="{{StudentBlogModel.author.profile.profile_pic.url}}"alt="profile"class="img-sm rounded-circle mb-5 mx-2">
          {% else %}
                <img src="{% static '..default-image' %}" alt="profile" class="img-sm rounded-circle mb-5 mx-2">
          {% endif %}
          

          你应该试试这个……

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 2021-04-30
            • 2012-05-20
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2016-01-02
            相关资源
            最近更新 更多