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)