Reputation: 647
My code works fine and I do migration to add avatar to my user.
But when I signup, avatar field does not show.
this is my code: models.py
from django.db import models
from django.contrib.auth.models import AbstractUser
def get_path_name(instance, filename):
path = 'media/avatar/'
name = instance.user.id + "-" + instance.user.email
path = path + name
return path
# custom User model
class CustomUser(AbstractUser):
avatar = models.ImageField(upload_to= get_path_name, blank=True, null=True)
my form:
from django.contrib.auth import get_user_model
from django.contrib.auth.forms import UserCreationForm, UserChangeForm
from django.core.files.images import get_image_dimensions
from django import forms
class CustomUserCreationForm(UserCreationForm):
class Meta:
model = get_user_model()
fields = ('email', 'username', 'avatar')
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
class CustomUserChangeForm(UserChangeForm):
class Meta:
model = get_user_model()
fields = ('email', 'username', 'avatar')
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
my template:
{% extends '_base.html' %}
{% load crispy_forms_tags %}
{% block title %}
{% endblock title %}
{% block content %}
<h2>Sign Up</h2>
<form method="post">
{% csrf_token %}
{{ form|crispy }}
<button class="btn btn-success" type="submit">Sign Up</button>
</form>
{% endblock content %}
my settings:
AUTH_USER_MODEL = 'accounts.CustomUser'
What I had missed as my sign up page is like that without avatar?
I tried to add field in template but seems not working. When I go to admin site I don t see avatar field but migrations had been done...
Thanks for any help
Upvotes: 1
Views: 326
Reputation: 647
I find my answer . Add this in settings.py
ACCOUNT_SIGNUP_FORM_CLASS = 'myapp.forms.CustomUserCreationForm'
Upvotes: 1