I'm having a problem trying to generate a CSV file with all registered users for export.
def descargar_usuarios(request):
perfiles = Perfil.objects.all()
response = HttpResponse(content_type='application/csv')
with open('usuarios.csv','wb') as f:
writer = csv.writer(f)
columnas = []
writer.writerows(['usuario','nombre','apellido','correo','boutique'])
for p in perfiles:
columnas = [p.usuario.username,p.usuario.first_name,p.usuario.last_name,p.usuario.email,p.boutique]
writer.writerows(columnas)
writer.save(response)
return response
I try to write all the columns in one go but I can't find a way.
Once I have it, I don't want to save it on the server, but download it from the browser.
To download a csv you not only have to specify the content_type , but also the header.
with this you will download it in the browser
you can see more details in the official documentation