Django - Image Upload

To upload image in server using Django perform the following code:

django-admin startproject imageupload
cd imageupload
python startapp appimage
add 'appimage' in INSTALLED_APPS
add 'templates' in TEMPLATES
MEDIA_URL = '/media/' 
media folder will automatically created when you add image.
from django.db import models  
class Image(models.Model):  
    caption = models.CharField(max_length=200)  
    img = models.ImageField(upload_to='images')  
    def __str__(self):  
        return self.caption
Install the pillow library, which helps work with the images.
pip install Pillow (app)
from django.urls import path
from . import views

urlpatterns = [
    path('image_upload', views.imageview, name = 'image_upload'),
    path('success', views.success, name = 'success'),
	path('view_images', views.displayimages, name='view_images'),
] (project)
from django.contrib import admin
from django.urls import path,include
from django.conf import settings  
from django.conf.urls.static import static

urlpatterns = [
	path('', include('appimage.urls')),

if settings.DEBUG:  
    urlpatterns += static(settings.MEDIA_URL,document_root=settings.MEDIA_ROOT) (app)
from django.http import HttpResponse
from django.shortcuts import render, redirect
from .forms import *

# Create your views here.
def imageview(request):
	if request.method == 'POST':
		form = ImageForm(request.POST, request.FILES)

		if form.is_valid():
			return redirect('success')
		form = ImageForm()
	return render(request, 'file.html', {'form' : form})

def success(request):
	return HttpResponse('successfully uploaded')
def displayimages(request):
	if request.method == 'GET':
		Images = Image.objects.all() 
		return render(request, 'file2.html',{'images' : Images}) (app)
from django import forms
from .models import *
class ImageForm(forms.ModelForm):
    class Meta:
        model = Image
        fields = ['caption', 'img']
python makemigrations
python migrate  

from .models import Image
# Register your models here.