I have UserProfile model that I instantiate whenever a User is created by using post_save signal everything is working fine except the ImageField in the userprofile. I am using django-allauth for signup and login. When I try to access the profile page of any user, the console repeatedly prints this :
[28/Dec/2016 12:22:06] "GET /media/C%3A/Users/shagu/Desktop/zorion-develop/project_1
/media/profile/images/Users/shagu/Desktop/zorion-develop/project_1/media/profile/images/...
and says this is the end
../zorion-develop/project_1/media/profile/images/nobody.jpg HTTP/1.1" 302 0
[28/Dec/2016 12:24:26] code 414, message Request-URI Too Long
[28/Dec/2016 12:24:26] "" 414 -
The profile model is as follows :
class UserProfile(models.Model):
user = models.OneToOneField(User, null=True, blank=True, on_delete=models.CASCADE)
image = models.ImageField(width_field="width_field",
height_field="height_field",
upload_to=os.path.join(settings.MEDIA_ROOT,'media', 'profile','images'),
default = os.path.join(settings.MEDIA_ROOT,'media', 'profile','images', 'nobody.jpg'),
)
height_field = models.IntegerField(default=0)
width_field = models.IntegerField(default=0)
age = models.IntegerField(null=True, blank=True)
gender = models.CharField(
choices=(
('M', 'male'),
('F', 'female'),
),
default='M', max_length=1)
profile_confirmed = models.BooleanField(default=False)
settings related to media are as follows :
MEDIA_ROOT = os.path.join(os.path.dirname(PROJECT_ROOT))
MEDIA_URL = "/media/"
And this is how I am trying to access the image in my template :
<div align="center"><img alt="User Pic" src="{{ user.userprofile.image.url }}" id="profile-image1" class="img-circle img-responsive">
I am kinda new to django. Please let me know where I am messing up.
There is just little less info that needed to fully understand and fix your problem, but I can give you a hint that will probably fix it.
First of all, your MEDIA_ROOT in the current state points outside of your project, because PROJECT_ROOT is probably something like '/home/user/myproject' and MEDIA_ROOT will be '/home/user'. Anyway that's fine if you actually wanted it to be. But actually I would change it to:
Second, using absolute path in your
is not actually a good practice. You should use relative path and Django will actually build correct path (and link in the future) relatively to MEDIA_ROOT and MEDIA_URL. Like this:
After you do supposed fixes, upload a new file/image for your image field, because I suppose Django already store links in your db in a wrong way and highly recommend clean testing.