Django File Uploading | Django Tutorial for Beginners
August 11, 2021 2022-12-17 23:42Django File Uploading | Django Tutorial for Beginners
Django File Uploading | Django Tutorial for Beginners
Django File Uploading (Images, doc files, video, audio, etc)
Are you want to work with Django file uploading form Or do you want to understand how we can upload different files like images, audio, video, pdf, doc files, etc on the Django website? So you are in the right place, you can develop a website where users will able able to upload any kind of files to the server.
Django is the python based web application or web framework that is the most famous and trending one. There are different topics related to Django we cleared with examples on the JafriCode platform. Now, we are ending to Django, as we have mentioned today, we have to learn the Django file uploading system, in which we will show you how you can make a user form to collect different files (image, video, audio, etc) from the user to save the database. You will learn easily if you read this lesson from start to end.
What is Python Django Framework?
Django follows the MVT (Modle View Template) architectural design pattern, which is written in Python’s high-level programming language. Django is used to build high-quality, secure, User-friendly, maintainable, and good performance websites or web applications. There are predefined modules, functions in Django which help developers to build a rapid website or application.
Working with Django File Uploading Form
To work with the media file you need to set in the settings.py file, for that, copy and paste the following code of line into your project settings.py file.
MEDIA_ROOT = os.path.join(BASE_DIR, ‘media’) # it will create a root folder with ‘media’ name, all the media folders and files will save in that folder.
MEDIA_URL = ‘/media/’ # it will create a folder including ‘media’ word in the URL for media files
Follow the following steps to work with Django file uploading form
You have to open the models.py file to upload different files in your project.
from django.db import models
class JafriCode_File(models.Model):
# To upload image
image = models.CharField(max_length=255, blank=True)
# To upload document
document = models.FileField(upload_to=’documents/’)
# To upload video
video = models.FileField(upload_to=’video/’)
# To upload audio
audio = models.FileField(upload_to=’audio/’)
1. Then you have to run a command to create the migration file,
python manage.py makemigrations , it will create a migration file, then you have to run migrate command to apply changes on migration file, that creates a final table in the database
So, run the following command to create a database table, according to your migration file
python manage.py migrate
2. As you have created a model for displaying a form, from that user will be able to upload any kind of file to the Django server. So create a file named forms.py in your application folder. Then create a ModelForm to show form the field to a user, that helps to store data in the database easily.
3. After creating ModelForm, you need to import in views section, to render on the frontend.
4. Now, you need to render the form in the template, where you can more customization with frontend design. If you are using file field or input file field, then you have to use enctype=”multipart/form-data” attribute in the form tag,
<form method=“POST” class=“jafricode_class” enctype=“multipart/form-data”>
{% csrf_token %}
{{ form}}
<button type=“submit” class=“btn btn-default”>Submit</button>
</form>
5. All is done, open web page URL after run server project, you will get a form having the field to the input file, surely, code will work properly, if anything else errors or issue then post a comment to discuss with us
Now! what you have to do?
As you have understood this article about “Django File Uploading”, now you must follow instructions as discussed in this article. In case of any issues or problems, don’t worry, you can discuss them with us.
On other hand, If you have any suggestions or knowledge about this article, you can share it with us, we will appreciate you!
If this article is good then share it on Facebook, Twitter, Pinterest, Instagram, etc.
Advanced Tutorials for Beginners
What is Django Cache | Django Tutorial for Beginners
Build a Django CRUD Application | Django Tutorials
What is Django Cookies | Django Tutorials
Why Django Framework is liked by Beginners?
What Programming Language is used for Data Analytics