title: convert image from base64 to image and save in database in django
body: <p>I'm trying export a string base64 that represents an image taken from canvas html5, this is the code in javascript:</p>
  
  <pre><code>var canvas1 = $("#canvas1")[0];
  var ctx1 = canvas1.getContext('2d');
  dataURL1 = canvas1.toDataURL();
  </code></pre>
  
  <p>Whit ajax I sent the image to server:</p>
  
  <pre><code>$.ajax({
      type: "POST",
      url: "/regiter_respuesta_agilidad/",
      data:{
          'imagen1': dataURL1,
      }
  });
  </code></pre>
  
  <p>In views.py of django I use base64.b64decode to export the string to image:</p>
  
  <pre><code>imagen1 = request.POST['imagen1']
  image_data = base64.b64decode(imagen1)
  imagene = ContentFile(image_data, 'imagen1.png')
  answer = Answer(imagen=imagene)
  </code></pre>
  
  <p>In models.py I have:</p>
  
  <pre><code>class Answer(models.Model):
      imagen = models.ImageField(upload_to=url)
  </code></pre>
  
  <p>The Problem is when the image is saved, the file imagen1.png is corrupted and I can't open it, Can someone help me with this problem?, or is there another way to do this? thank you very much.</p>