0

My form modifForm have this field :
image = forms.ImageField(widget=forms.FileInput(attrs={'id':'log'}))

My template HTML :

        <form method="POST" id="formMod" action="{% url 'modification' %}">
        {% csrf_token %}
        {{ form.image }}
        ...

My method ajax recupered the value of this image and send it to a view named "modification" (url : localhost:8000/modification) :

 function modification(){
      var log = document.getElementById("log").value;
      $.post("modification",{"image":log});
      window.location.href = window.location.href;
      window.location.reload();
    }

But the value of variable log is the URL of the image and not a ImageField ! So when this function js "modification" send this the name of image to my view, I want create a ImageField with this name in my view because I have a model which have a imagefield in attribute ! But I don't know how make...

My view :

def modification(request):
    image = requete.get("image")
    if image_du_projet != None : 
         #image is the name of the image ("name.jpeg"), how create here imagefield with this name ?

Finally, how create Imagefield with image name in view ?

Thank you

Edit :

I tried this but it don't works :

        from django.core.files.uploadedfile import SimpleUploadedFile
        file_contents = SimpleUploadedFile("%s" %("img.jpeg"), request.raw_post_data)
        log = ModeleWithImageField.objects.create()
        log.image.save("img.jpeg", file_contents, True)
YassVegas
  • 179
  • 6
  • 22
  • This has been answered here: http://stackoverflow.com/questions/1308386/programmatically-saving-image-to-django-imagefield?rq=1 and here: http://stackoverflow.com/questions/1393202/django-add-image-in-an-imagefield-from-image-url – xyres Jul 31 '15 at 18:11
  • I make a error, is not a url just the name of image ! :( I modified my post – YassVegas Jul 31 '15 at 18:20
  • For example, is can be "tree.jpeg" – YassVegas Jul 31 '15 at 18:26

0 Answers0