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