MD Sulaiman
MD Sulaiman

Reputation: 175

Why image is not getting saved in media directory in django?

I am trying to save the image in media/images directory and have done each and every neccosry step which is described below. But after all, I am not getting the image in my directory please someone guide me where is the problem. Thank You.

Models.py

from django.db import models

# Create your models here.

class students(models.Model):
    firstname = models.CharField(max_length=50)
    lastname = models.CharField(max_length=50)
    fathername = models.CharField(max_length=50)
    city = models.CharField(max_length=50)
    country = models.CharField(max_length=50)
    state = models.CharField(max_length=50)
    zip = models.IntegerField(blank=True)
    address = models.CharField(max_length=100)
    contact =models.CharField(max_length=20)
    photo = models.ImageField(upload_to='images/', height_field=None, width_field=None, max_length=None)

urls.py

from django.urls import path, include
from home import views
from django.contrib import admin
from home import StudentViews
from django.conf.urls.static import static
from django.conf import settings



urlpatterns = [
    path("", views.index, name="index"),
    path("Add_Student", views.Add_Student, name="Add_Student"),
    path("display_students", views.Display_Student, name="display_students"),
    path("<int:id>", views.student_profile, name="student_profile"),
    #path("student/<int:id>/view", views.student_profile, name="student_profile"),

    path("Student_Home", StudentViews.StudentHome, name="Student_Home")
    
]

#this line is for media 
urlpatterns += static(settings.MEDIA_URL, document_root = settings.MEDIA_ROOT)

settings.py

In settings.py, I added the following lines of code.

MEDIA_ROOT= os.path.join(BASE_DIR, 'media/images')
MEDIA_URL= "/images/"

My Directory

Student_Management_System

Note: I have already installed Pillow.

views.py

Here is my view Add_Student that add the entry into database.

from django.shortcuts import render, redirect
from home.models import students

# Create your views here.

def index(request):
    return render(request, "index.html")

def Add_Student(request):
    if request.method == 'POST':
        firstname = request.POST.get('firstname')
        lastname = request.POST['lastname']
        fathername = request.POST['fathername']
        city = request.POST['city']
        country = request.POST['country']
        state = request.POST['state']
        zipcode = request.POST['zip']
        address = request.POST['address']
        contact = request.POST['contact']
        photo = request.POST['photo']

        Add_Student = students.objects.create(firstname=firstname, lastname=lastname, fathername=fathername, city=city,country=country, state=state, zip=zipcode, address=address, contact=contact, photo=photo)
        Add_Student.save()
        return redirect('Add_Student')
    else:
        return render(request, 'student/Add_Student.html')

Upvotes: 2

Views: 2149

Answers (1)

Prakhar
Prakhar

Reputation: 1014

Update your MEDIA_ROOT

MEDIA_ROOT= os.path.join(BASE_DIR, 'your_app_name/media')

You can read from the documentation point number 2

defining the upload_to option specifies a subdirectory of MEDIA_ROOT to be used for uploaded files.

Also make sure that you have added enctype="multipart/form-data"form attribute to your template as mentioned here in the documentation

Note that request.FILES will only contain data if the request method was POST and the that posted the request has the attribute enctype="multipart/form-data". Otherwise, request.FILES will be empty.

Also a little change in Add_student view

photo = request.FILES['photo']

Add_Student.save() # NO need to use this. create() creates and saves the object at the same time.

You can read about this here

Upvotes: 3

Related Questions