What is Django Filefield?
Django Filefield allows us to upload files using models. We already have discussed a lot about model fields in django for storing text, number, date data.Like static folder, we create an media folder inside project to store uploaded files.
If you don’t know about i recommend you to click on the link below:
Now the question arises that, how we can upload files or images using django models. This article answers your question that, Yes!! we can do so very easily. For this reason use FileField or ImageField. Both these fields has same syntax as shown below:
model_obj = (upload_to=path, max_length=100, **options)
There are various common parameters of Image Field or File Field, like unique, blank etc. But there are two special optional parameters which are given below:
It is an attribute to set where file should be save. There we need to set an MEDIA_ROOT in settings.py file of your project. After that, there is an MEDIA_URL which refers to the path, defined in media root. After that you can define a path where image/file should be save.For example if you set upload_to =’media/’, then it would be save in media folder of your project. i.e:
It is another optional attribute for File Field.It is the number of character(name of file) a file field will save. Its default value is 100, but you can set it to any integer like Char Field.
- Add MEDIA_ROOT variable to the settings.py file of your project. It stores full path in system, where you want Django to store uploaded files.
- Set MEDIA_URL in same (settings.py) file. It specifies a URL for MEDIA_ROOT.
Don’t Forget to install ‘Pillow’
If you are using any file field or image field, you need to install a module named ‘Pillow‘. We can say that pillow module allows django to upload/ accept a file in bit stream. We can install ‘Pillow‘ using ‘pip‘. Here is command to install Pillow:
pip install Pillow
If you have not module ‘Pillow‘. There will be following error. Moreover you can see here how to install Pillow:
Django Filefield Example
MEDIA_URL = '/media/' MEDIA_ROOT = os.path.join(BASE_DIR,'media/')
from django.db import models class Profile(models.Model): username = models.CharField(max_length=50) email = models.EmailField(unique=True) profile_pic = models.ImageField(upload_to='images/',null=True) resume= models.FileField(upload_to='resume/',null=True) def __str__(self): return self.username
from django.contrib import admin from firstapp.models import Profile admin.site.register(Profile)
Media Folder -> After uploading files
Another method of uploading files
As you can see in the above example, by defining MEDIA_ROOT we can easily set path where to upload data. It saves a data in root directory, Moreover automatically creates specified folder inside media folder. In example above, it automatically creates images and resume folder inside media folder.
If you want to save data in more effective way, django let you do same. You can save data according to the date and year. Change upload_to variable of the FileField as shown below:
profile_pic = models.ImageField(upload_to='images/%Y/%m/%d',null=True)
I have just added this to our current model.The data is now save as following way:
static vs media folder
You may notice that we uses static folder to save images and other files. Now question arises that why django needs MEDIA_ROOT? and why it does not use STATIC_DIR instead.
The simple answer is that, django stores information, modules and objects in very structured way. So to avoid confliction between website’s static content and media content, it refers to create two different folders.
Static folder holds the static content of website where media folder contains the user’s uploaded data. Moreover, For static folder there is STATIC_URL and STATICFILES_DIRS, where for media folder there is MEDIA_URL and MEDIA_ROOT.
Download source code
So it is all about the Django Filefield. In our further tutorials we will discuss about how to take input from user and save it to Database. Thanks!!!