Template image does not upload

Asked

Viewed 84 times

1

Hello, I’m developing an app that needs the user to upload their profile image, but the 'upload_to' mechanism of the Customuser ft field apparently isn’t working. The image simply does not go to the target folder, nor does it create the folder with the desired name, in the case of 'photos', as it should. I’ve tried to recreate it and nothing.

py.models

#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 register 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})

views.py of the register:

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})
  • it would be good if you also enter your view code

  • I put this code to change the photo with the registration already done and the mechanism to save the photo in the register stopped.def fotoPerfil_change(request, id):&#xA; customuser = get_object_or_404(CustomUser, pk=id)&#xA; form = ChangePhoto(request.POST or None, request.FILES or None, instance=customuser)&#xA; if form.is_valid():&#xA; form.save()&#xA; suc = "Foto de perfil atualizada com sucesso!"&#xA; return render(request,'perfil/fotoperfil.html', {'form':form, 'suc': suc})&#xA; return render(request, 'perfil/fotoperfil.html', {'form': form})

  • The previous code refers to the image exchange mechanism, now follows the views.py def register(request):&#xA; if request.method == 'POST':&#xA; form = CustomUserCreationForm(request.POST or None, initial={&#xA; 'choice': True})&#xA; if form.is_valid():&#xA; user = form.save()&#xA; user.choice = 0&#xA; user.set_password(user.password)&#xA; form.save()&#xA; return render(request, 'register-sucess.html', {'user': user})&#xA; else:&#xA; form = CustomUserCreationForm()&#xA; return render(request, 'register-f.html', {'form': form})

  • it would be better if you put the code in the question, in the same way as you did with the code of Forms, models and template. Here in the commentary it is very difficult to read the code

  • Okay, it’s really clearer now.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.