How to save image in Django project

Do you want to Save image in django then this is the right article for you. I will walk you through how you can implement saving image in django effectively.

Step involves in saving images in Django are :

  • Step 1: Setting up your Django Project
  • Step 2: Creating a Django App
  • Step 3: Define a Model
  • Step 4: Creating a Form
  • Step 5: Writing a View
  • Step 6: Create a Template
  • Step 7: Configuring Media Settings
  • Step 8: URL Configuration
How to Save images in Django project.

Step 1: Setting up your Django Project

First thing make sure that your Django project is been set up. In case you don’t know let’s start with installing Django using pip.

pip install django

After the installation let’s create a Django project.

django-admin startproject myproject

Now you have to enter into the folder where you created the django by using cd folder

cd myproject

Then after you should be in your project directory then you should move to step 2

Step 2: Creating a Django App

Since Django project is organized into apps let’s create an app by writing the following codes.

python manage.py startapp myapp

After creating the app, add it to your installed app, then run migrations.

Step 3: Define a Model

from django.db import models
class MyImage(models.Model):   image = models.ImageField(upload_to='images/')

Step 4: Creating a Form

You need to create a form.py in your Django app directory then create a form inside of it .

from django import formsfrom .models import MyImageclass ImageForm(forms.ModelForm):
class Meta:    
model = MyImage
fields = ['image']

Step 5: Writing a View

Navigate to your views.py then create the following views below.

from django.shortcuts import render, redirect
from .forms import ImageForm

def upload_image(request):
    if request.method == 'POST':
        form = ImageForm(request.POST, request.FILES)
        if form.is_valid():
            form.save()
            return redirect('success')
    else:
        form = ImageForm()
    return render(request, 'upload_image.html', {'form': form})

Step 6: Create a Template , after creating views then create a template then write the following code below

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Upload Image</title>
</head>
<body>
    <h2>Upload Image</h2>
    <form method="post" enctype="multipart/form-data">
        {% csrf_token %}
        {{ form.as_p }}
        <button type="submit">Upload</button>
    </form>
</body>
</html>

Step 7: Configuring Media Settings

Navigate to your settings.py then set your media and static files as my own below.

MEDIA_URL = '/media/'<br>MEDIA_ROOT = os.path.join(BASE_DIR, 'media')

Step 8: URL Configuration to save image in django

Go to your Django url.py you have created before then add the following code in your urls.py

from django.conf import settings
from django.conf.urls.static import static

urlpatterns = [
    # Your other URL patterns here
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

That is it rerun your Django project by running

python manage.py makemigrations
python manage.py migrate
python manage.py runserver

That is it, you can now save your save your Django images.

Leave a Reply

Your email address will not be published. Required fields are marked *