【问题标题】:ValueError at /profile/:The 'image' attribute has no file associated with it/profile/ 处的 ValueError:“图像”属性没有与之关联的文件
【发布时间】:2019-02-19 21:17:41
【问题描述】:

当用户注册我的应用程序时。当他到达个人资料页面时我收到此错误

 'ValueError at /profile/:The 'image' attribute has no file associated with 
  it.'

这是我的个人资料模型:

class Profile(models.Model):
Full_Name = models.CharField(max_length=32,blank=True)
Name = models.OneToOneField(settings.AUTH_USER_MODEL,on_delete=models.CASCADE)
E_mail = models.EmailField(max_length=70,blank=True)
Qualification = models.CharField(max_length=32,blank=True)
Permanant_Address = models.TextField(blank=True)
image = models.ImageField(upload_to='user_images', null=True, blank=True)

def __str__(self):
    return str(self.Name)


def get_absolute_url(self):
    return reverse("userprofile:profiledetail")

@property
def image_url(self):
    if self.image and hasattr(self.image, 'url'):
        return self.image_url

这是我的表格:

class profileform(forms.ModelForm)"


class Meta:
    model = Profile
    fields = ('Full_Name','Name', 'E_mail','Qualification','Permanant_Address','image')

这是我的看法:

from django.shortcuts import render
from django.views.generic import DetailView,UpdateView
from django.contrib.auth.mixins import LoginRequiredMixin
from userprofile.models import Profile
from userprofile.forms import profileform

# Create your views here.

class profiledetailview(LoginRequiredMixin,DetailView):
    context_object_name = 'profile_details'
    model = Profile
    template_name = 'userprofile/profile.html'

    def get_object(self):
       return self.request.user.profile

  class profileupdateview(LoginRequiredMixin,UpdateView):
     model = Profile
     form_class = profileform
     template_name = 'userprofile/profile_form.html'

     def get_object(self):
         return self.request.user.profile

在我的模板中我做了这样的事情:

<img class="profile-user-img img-responsive img-circle" src="{{ 
  profile_details.image.url|default_if_none:'#' }}/" alt="User profile 
  picture">

我一直在阅读有关内置模板标签和过滤器的文档,我认为这里有一个解决方案,但我认为我似乎无法正确使用模板标签。

如何配置此模板以使图片成为选项。如果他们没有图片,请留下它,但显示人名。

谢谢

【问题讨论】:

    标签: django django-models django-forms django-filter django-template-filters


    【解决方案1】:

    您正在尝试获取不存在的图像的 url。

    基本上,如果您要检查图像是否存在,则必须这样做:

    if profile_details.image:
        url = profile_details.image.url
    

    或者在你的情况下:

    src={% if profile_details.image %}{{ profile_details.image.url }}{% else %}#{% endif %}
    

    【讨论】:

    • 谢谢它的工作......但我已经给出了标签之外的条件并且它工作得很好@Navid2zp
    • 是的..谢谢你的回复@Navid2zp
    • 我看了这一篇,忘了把if语句末尾的.url删掉,然后就成功了。
    【解决方案2】:

    我亲爱的朋友,Navid 的解决方案很好但还不够,因为如果用户没有头像,您应该轻松显示默认图像(不需要迁移)。因此,您可以按照以下步骤操作:

    将此方法添加到您的人物模型中:

    @property
    def get_photo_url(self):
        if self.photo and hasattr(self.photo, 'url'):
            return self.photo.url
        else:
            return "/static/images/user.jpg"
    

    您可以使用任何路径(/media、/static 等),但不要忘记将默认用户照片作为 user.jpg 放入您的路径。

    并在模板中更改您的代码,如下所示:

    <img src="{{ profile.get_photo_url }}" class="img-responsive thumbnail " alt="img">
    

    【讨论】:

      【解决方案3】:

      如果您的错误来自 HTML 而不是管理站点,请这样做。

      {% if profile_details.image %} # make sure that it's not .url in the end
        <img src="{{ profile_details.image.url }}" alt="{{ profile_details.title }}">
      {% else %} 
        <p>No Image</p>
      {% endif %}
      

      更改 profile_details,如果您的上下文名称不同,

      示例

      context = {
              'products': products,
          }
      

      如果您的 ImageField 名称不同,也可以更改图像

      示例:

      thumbnail = models.ImageField(upload_to='uploads/', blank=True, null=True, )
      

      <!-- For me it looked like this -->
      
         {% for product in products %}
           {% if product.thumbnail %} 
             <img src="{{ product.thumbnail.url }}" alt="{{ product.title }}">
           {% else %} 
             <p>No Image</p>
           {% endif %}
         {% endfor %}
      

      【讨论】:

        【解决方案4】:

        非常简单!这就是我所做的:我删除了所有没有附加图像的旧帖子,制作了一个新帖子,效果很好。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2023-02-21
          • 2021-02-11
          • 2020-03-08
          • 2020-08-11
          • 1970-01-01
          • 2016-02-06
          • 2018-04-28
          • 1970-01-01
          相关资源
          最近更新 更多