im following Add image/avatar to users in django tip to have users upload their profile pictures, and to display the user's profile picture when a user log in.
Adding profile picture to django user
But for some reason, it comes with an error saying "Creating a ModelForm without either the 'fields' attribute or the 'exclude' attribute is prohibited; form UserProfileForm needs updating."
Is there anyway i can directly modify the django.contrib.auth.models's User model so that i can add only the profile picture and have it displayed on index.html?
there is not a whole lot of information regarding this profile picture system.
and it's only been days since i started learning about django and python.
could anyone explain with examples, how i can achieve this?
Thanks.
(this is the forms.py that's implementing the tip above)
from django import forms
from django.contrib.auth.models import User
from django.contrib.auth import authenticate, get_user_model, login, logout
class UserSignupForm(forms.ModelForm):
email = forms.EmailField(label='Confirm Email')
password = forms.CharField(widget=forms.PasswordInput)
class Meta:
model = User
fields = [
'username',
'email',
'password'
]
def clean_email(self):
email = self.cleaned_data.get('email')
email_qs = User.objects.filter(email=email)
if email_qs.exists():
raise forms.ValidationError("This email has already been registered")
return email
from django.core.files.images import get_image_dimensions
from .models import UserProfile
class UserProfileForm(forms.ModelForm):
class Meta:
model = UserProfile
def clean_avatar(self):
avatar = self.cleaned_data['avatar']
try:
w, h = get_image_dimensions(avatar)
#validate dimensions
max_width = max_height = 100
if w > max_width or h > max_height:
raise forms.ValidationError(
u'Please use an image that is '
'%s x %s pixels or smaller.' % (max_width, max_height))
#validate content type
main, sub = avatar.content_type.split('/')
if not (main == 'image' and sub in ['jpeg', 'pjpeg', 'gif', 'png']):
raise forms.ValidationError(u'Please use a JPEG, '
'GIF or PNG image.')
#validate file size
if len(avatar) > (20 * 1024):
raise forms.ValidationError(
u'Avatar file size may not exceed 20k.')
except AttributeError:
"""
Handles case when we are updating the user profile
and do not supply a new avatar
"""
pass
return avatar