When I submit all the fields are updated except for the profile photo, it does not generate any error, it simply does not load the image
The request.POST returns the following in a print that I place in the post method
<tr><th><label for="id_foto_perfil">Foto de perfil:</label></th><td><input type="file" name="foto_perfil" accept="image/*" id="id_foto_perfil"></td></tr>
Note: by Django admin I can upload the image without problems
The model where I upload the image is this
class User(AbstractUser):
foto_perfil = models.ImageField(upload_to='usuarios/perfil', null=True, blank=True, verbose_name='foto de perfil')
#otros campos
This is the view I am using
class PerfilUpdateView(UpdateView):
model = Empleado
model_user = User
form_class = PerfilForm
form_class_user = PerfilUsuarioForm
template_name = 'talento_humano/perfil.html'
def get_object(self, queryset=None):
return Empleado.objects.get(usuario=self.request.user)
def get_context_data(self, **kwargs):
context = super(PerfilUpdateView, self).get_context_data(**kwargs)
empleado = self.get_object()
usuario = self.model_user.objects.get(id=empleado.usuario_id)
if 'form' not in context:
context['form'] = self.form_class()
if 'form_user' not in context:
context['form_user'] = self.form_class_user(instance=usuario)
context['id'] = empleado.pk
return context
def post(self, request, *args, **kwargs):
self.object = self.get_object
empleado = self.get_object()
usuario = self.model_user.objects.get(id=empleado.usuario_id)
form = self.form_class(request.POST, instance=empleado)
form_user = self.form_class_user(request.POST, instance=usuario)
print(form_user)
if form.is_valid() and form_user.is_valid():
# empleado = form.save(commit=False)
# empleado.usuario = form_user.save()
form_user.save()
empleado.save()
return HttpResponseRedirect(self.get_success_url())
else:
return self.render_to_response(self.get_context_data(form=form, form_user=form_user))
def get_success_url(self):
return reverse_lazy('perfil') + '?ok'
And these are the forms
class PerfilForm(forms.ModelForm):
class Meta:
model = Empleado
fields = ['titulo_obtenido', 'carrera_profesional']
class PerfilUsuarioForm(UserChangeForm):
password = None
class Meta:
model = User
fields = ('foto_perfil', 'first_name', 'last_name', 'celular', 'email')
And this is the form of the template
<form method="post" enctype="multipart/form-data">{% csrf_token %}
{{ form_user|crispy }}
{{ form|crispy }}
<br>
<button type="submit" value="Actualizar" class="btn btn-primary">
<i class="ace-icon fa fa-check bigger-120"></i>Actualizar
</button>
<a href="{% url 'mi_agenda.listar' %}" class="btn btn-default">Ir a Mi Agenda</a>
</form>
I think that in addition to the request.POST, you would need to add request.FILES in the form.m which would change from:
a
Once I used it like this, I got the info from here: https://simpleisbetterthancomplex.com/tutorial/2016/08/01/how-to-upload-files-with-django.html
Ah, another thing, I think you should also add the form.save() before or after the form_user.save()
Best regards