Django Framework

@jit dhulam
4 min readApr 8, 2019

--

9.Image /File upload in django at admin side

It is generally useful for a web app to be able to upload files (profile picture, songs, pdf, words…..). Let’s discuss how to upload files in this chapter, at the end of this you will learn the concepts behind Django file upload and how to handle file upload at admin site

When files are submitted to the server, the file data ends up placed in request.FILES.It is mandatory for the HTML form to have the attribute enctype="multipart/form-data" set correctly. Otherwise the request.FILES will be empty.

The form must be submitted using the POST method.

Django have proper model fields to handle uploaded files: FileField and ImageField.The files uploaded to FileField or ImageField are not stored in the database but in the filesystem.

FileField and ImageField are created as a string field in the database (usually VARCHAR), containing the reference to the actual file.

If you delete a model instance containing FileField or ImageField, Django will not delete the physical file, but only the reference to the file.

The request.FILES is a dictionary-like object. Each key in request.FILES is the name from the <input type="file" name="" />.

Each value in request.FILES is an UploadedFile instance.

You will need to set MEDIA_URL and MEDIA_ROOT in your project’s settings.py.

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

In the development server you may serve the user uploaded files (media) using django.contrib.staticfiles.views.serve() view.

urls.py

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

urlpatterns = [

# Project url patterns...
] if settings.DEBUG:
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

models.py

from django.db import modelsclass images(models.Model):
id_no=models.IntegerField()
name=models.CharField(max_length=20)
loc=models.CharField(max_length=20)
image=models.ImageField(upload_to='images')
profile=models.FileField(upload_to='files')

def __str__(self):
return self.name

FileField upload_to Parameter:

Note the upload_to parameter. The files will be automatically uploaded to MEDIA_ROOT/documents/.
ImageField parameter used to upload the only image files while FileField used to upload the various file (profile picture, songs, pdf, words…..)including images also

admin.py

from django.contrib import admin
from .models import images
class AdminImages(admin.ModelAdmin):
list_display = ['id_no','name','loc','image','profile']

admin.site.register(images,AdminImages)

Thats all about the code part, now run the makemigrations , migrate , createuser and runserver command , so its create the schema of database in mysql , with one super user to access the admin page of django.

Login to admin page to add the data, as below

Click to “+add” to insert data as below

Save the data by pressing the save button , after succesful saving data it goes to home page you will find below structure ,or Click on table name(Imagess) to see the contet ,you will find as below

The recent first record is added one .It shows the link of Images and Profile just click on the Images link you can see it in browser of next tab as below

Click on file link it download to your download folder.

Now This is all about the coding and working flow of the django image/file update , Lets see how it store .

Where Django Store The Files :

As mentioned above FileField and ImageField are created as a string field in the database (usually VARCHAR), containing the reference to the actual file.But not the actual image or file, if not store actual file then how is retriving is happening ?

Answer is here , Django don’t strore actual Images/Files in database but it store location of actual Imgaes/Files into the database as VARCHAR which are traced from djnago Project.

Django create the one media (which describe in settings.py file )folder under the project and store the all files/imgaes in same folder as below

So red marks files are just we inserted above files .

Click here to see the code in Github

Thats all about the uploading the image/ file in django at admin site

--

--