/profile/ 处的 ValueError:'image' 属性没有与之关联的文件

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

当用户注册我的app.I时,当他到达个人资料页面时收到此错误

 '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">

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

我如何配置此模板才能使图片成为一个选项。如果他们没有照片留下它但显示人名。

谢谢

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

基本上如果你想检查图像是否存在,那么你必须这样做:

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

或者您的情况:

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

亲爱的朋友,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">

很简单!这就是我所做的:我删除了所有没有附加图像的旧 post,制作了一个新的并且工作正常。

如果您的错误来自 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 %}