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

Viewed 4935

When a user registers for my app.I receive this error when he reaches the profile page

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

This is my profile model:

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

This is my form:

class profileform(forms.ModelForm)"


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

This is my view:

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

And in my template I have done something like this:

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

I'm been reading the documentation for Built-in template tags and filters I think a solution here is to use and I think I can't seem to use template tag properly.

How can I configure this template to make picture an option. If their are no picture leave it but display the persons name.

Thank you

4 Answers

You are trying to get the url for an image that doesn't exists.

Basically if you're trying to check if an image exists, then you have to do it this way:

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

or in your case:

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

My dear friend, Navid's solving is good but not enough because If user hasn't profile picture you should show default image easily (not need migration). So you can follow below steps:

Add this method to your person model:

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

You can use any path (/media, /static etc.) but don't forget putting default user photo as user.jpg to your path.

And change your code in template like below:

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

If your error comes from the HTML and not the admin site, then do like this.

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

Change the profile_details, if your context is named differently,

Example

context = {
        'products': products,
    }

also change image, if your ImageField is named differently

Example:

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

very easy! this was what i did: i deleted all my old post that doesn't have an image attached to it, made a new one and it worked fine.

Related