Hello, I'm developing an app that requires the user to upload their profile image, but the 'upload_to' mechanism of the CustomUser ft field is apparently not working. The image simply does not go to the intended folder, nor does it create the folder with the desired name, in the 'photos' case, which how it should happen. I've tried to recreate it and nothing.
models.py
#TODO: MODEL ADMIN E PAI DOS DEMAIS MODELS DE LOGIN
class CustomUser(AbstractBaseUser):
email = models.EmailField(_('E-mail '), max_length=255, unique=True)
username = models.CharField(_('Nome de usuário '), max_length=15, unique=True)
choice = models.BooleanField(default=False)
ft = models.ImageField(upload_to='fotos', null=True, blank=True, default='../media/site/user.png')
date_joined = models.CharField(_('Data de início '), max_length=20, default=timezone.now())
is_active = models.BooleanField(default=True)
is_admin = models.BooleanField(default=False)
USERNAME_FIELD = 'email'
REQUIRED_FIELDS = ['username', 'choice']
objects = UserManager()
class Meta:
verbose_name = _('Usuário')
verbose_name_plural = _('Usuários')
def __str__(self):
return self.username
def has_perm(self, perm, obj=None):
return True
def has_module_perms(self, app_label):
return True
@property
def is_staff(self):
return self.is_admin
forms.py
class CustomCompanyCreationForm(UserCreationForm):
class Meta(UserCreationForm.Meta):
model = CustomCompany
fields = ('name', 'departamento', 'cnpj', 'ft', 'telefone','cep', 'endereco', 'cidade', 'uf', 'username', 'email'
)
pag.html
{% load widget_tweaks %}
{% for hidden_field in form.hidden_fields %}
{{ hidden_field }}
{% endfor %}
{% if form.non_field_errors %}
<div class="alert alert-danger" role="alert">
{% for error in form.non_field_errors %}
{{ error }}
{% endfor %}
</div>
{% endif %}
{% for field in form.visible_fields %}
<div class="form-group">
<hr>
{{ field.label_tag }}
{% if form.is_bound %}
{% if field.errors %}
{% render_field field class="form-control is-invalid placeholder=form.text.label" %}
{% for error in field.errors %}
<div class="invalid-feedback">
{{ error }}
</div>
{% endfor %}
{% else %}
{% render_field field class="form-control is-valid placeholder=form.text.label" %}
{% endif %}
{% else %}
{% render_field field class="form-control" placeholder=form.text.label %}
{% endif %}
{% if field.help_text %}
<small class="form-text text-muted">{{ field.help_text|safe }}</small>
{% endif %}
</div>
{% endfor %}
I put this code to change the photo with the registration already done and the mechanism to save the photo in the registry stopped.
views.py to change photo:
def fotoPerfil_change(request, id):
customuser = get_object_or_404(CustomUser, pk=id)
form = ChangePhoto(request.POST or None, request.FILES or None, instance=customuser)
if form.is_valid():
form.save()
suc = "Foto de perfil atualizada com sucesso!"
return render(request,'perfil/fotoperfil.html', {'form':form, 'suc': suc})
return render(request, 'perfil/fotoperfil.html', {'form': form})
Register views.py:
def register(request):
if request.method == 'POST':
form = CustomUserCreationForm(request.POST or None, initial={
'choice': True})
if form.is_valid():
user = form.save()
user.choice = 0
user.set_password(user.password)
form.save()
return render(request, 'register-sucess.html', {'user': user})
else:
form = CustomUserCreationForm()
return render(request, 'register-f.html', {'form': form})