2

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

  • home
  • media/images
  • templates
  • static

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')
2
  • Add your view class/function. Commented Jun 29, 2021 at 13:10
  • @JPG I have added views.py file, please check Commented Jun 29, 2021 at 14:31

1 Answer 1

3

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

Sign up to request clarification or add additional context in comments.

7 Comments

Thank you for your answer. As you noticed that I am using my own form rather than django built-in form. So in the upload_file() view, form = UploadFileForm(request.POST, request.FILES) request.FILES is used as described in the documentation. But in my case, where I have to use it?
Ok wait! Updating my answer
Thank you very much again. You are great.... It solved my problem. By the way, Django is very hard :D
Yes, I am working hard. Please keep in touch with me
Please answer this simple question. stackoverflow.com/questions/68334466/…
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.