upload images in Django save onlyto folder but not saving to mysql database

22 Views Asked by At

I need help my code below save image to folder but not saving to mysql database. pls i dont knw where am getting it wrong.

VIEW.PY

def indeximg(request):

if request.method == "POST":


    form=ImageForm(request.POST, request.FILES)
    
    
    if form.is_valid():
    
    
       form.save()
       
       
       return redirect('uploadok')
else:


   form = ImageForm()
   
   
   return render(request, 'indeximg.html', {'form': form})
   
   

def uploadok(request):

return HttpResponse(' upload successful')

IN MODEL.PY

class Image(models.Model):

caption=models.CharField(max_length=100)


image=models.ImageField(upload_to='images/')
1

There are 1 best solutions below

2
Yusuf Adel On

From django docs about ImageFied

ImageField instances are created in your database as varchar columns with a default max length of 100 characters. As with other fields, you can change the maximum length using the max_length argument.