mardi 29 juin 2021

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

  • home
  • media/images
  • templates
  • static

Note: I have already installed Pillow.




Aucun commentaire:

Enregistrer un commentaire