Upload image without django forms
mlkslgodels.py
models.py
class ImageUpload(models.Model):
image = models.ImageField(upload_to="images/", blank=True, null=True)
Note** :
python manage.py makemigrations
python manage.py migrate
index.html
<form action="" method="POST" enctype="multipart/form-data">
<input type="file" name="image"/>
<submit type="submit">submit</submit>
</form>
views.py
def index(request):
if request.method=="POST":
img=request.FILES["image"]
data=ImageUpload.objects.create(image=img).save()
return HttpResponse("Image Uploaded")
else: # for get request
return render(request,"index.html")
Comments
Post a Comment