mfnas.blogg.se

Django file upload example
Django file upload example









  1. #DJANGO FILE UPLOAD EXAMPLE INSTALL#
  2. #DJANGO FILE UPLOAD EXAMPLE CODE#

Add templates Create template with form submission Then, calling form.save() will save file object to the 'upload_to' location of FileField/ ImageField (defined in Tutorial data model). We can get form data simply just by assigning request.FILES to the file field of TutorialForm. Return render(request, 'tutorial/list.html', ) – deleteTutorial(): delete a Tutorial by its id ( pk).įrom django.shortcuts import render, redirect Otherwise, return to upload.html template. – uploadTutorial(): get form data from HTTP POST request, save form data to database and files to filesystem storage, render list.html template if the process is successful. – tutorialList(): get list of Tutorials from database, render list.html template. Inside tutorials/views.py, define some functions: fields attribute indicates the order of form fields specified by model field.įor more details about form field that is corresponding of each model field, please visit list of conversions. This form maps closely to Tutorial model that we’ve created before. Under tutorials app folder, create new file named forms.py:įields = Run the following Python script to apply the generated migration:Ĭheck MySQL Database, now we can see that a table for Tutorial model was generated and it’s named tutorials_tutorial: Call to migrations.CreateModel() method will create a table that allows the underlying database to persist the model. It has an operation for creating Tutorial model table.

#DJANGO FILE UPLOAD EXAMPLE CODE#

The generated code defines a subclass of the django.db.migrations.Migration. ('attachment', models.FileField(upload_to='tutorial/attachments/')), ('feature_image', models.ImageField(upload_to='tutorial/images/')),

django file upload example

('category', models.CharField(max_length=100)), ('title', models.CharField(max_length=100)), ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), It indicates that the tutorials/migrations/0001_initial.py file includes code to create Tutorial data model: Python manage.py makemigrations tutorials If we don’t do this, ONLY Tutorial information (title, category, file info) will be deleted from database, all files still exist. So we need to override Django Model delete() method, it helps us handle removing actual file/image from storage. The uploaded file will be located at MEDIA_ROOT/files/docs/5 for example.įileField and ImageField are saved as a string field in database that will be references to the real files/images. If you want to create more folders for datetime, you can use:ĭoc = models.FileField(upload_to='docs/%Y/%m/%d/') In the code, feature_image will be save to files/tutorial/images folder, and attachment will locate at files/tutorial/attachments. upload_to parameter specifies where the file/image will be stored. To handle uploaded files, we use model fields: FileField for attachment and ImageField for feature_image.Ĭategory = models.CharField(max_length=100)įeature_image = models.ImageField(upload_to='tutorial/images/')Īttachment = models.FileField(upload_to='tutorial/attachments/')įileField or ImageField will not help us store file/image in the database but in the filesystem. Our Tutorial data model is a Django Model with 4 fields: title, category, feature_image, attachment. So uploaded files will be accessed with root url /files/., and they are located at folder named files. MEDIA_ROOT = os.path.join(BASE_DIR, 'files') + MEDIA_URL: url for media (uploaded files) from MEDIA_ROOT. + MEDIA_ROOT: Absolute filesystem path to the directory that holds uploaded files. In project’s settings.py, set value for MEDIA_ROOT and MEDIA_URL:

django file upload example

Open gkzRestApi/settings.py, find INSTALLED_APPS, then add: This Django App will be used to upload, view and delete tutorials. Open tutorials/apps.py, we can see TutorialsConfig class (subclass of the ) that represents our Django app and its configuration: Run following commands to create new Django App named tutorials: Open DjangoUploadModelForm/settings.py and change declaration of DATABASES: Once the installation is successful, import this module in DjangoUploadModelForm/_init_.py:

#DJANGO FILE UPLOAD EXAMPLE INSTALL#

In this tutorial, we use pymysql: pip install pymysql. We have to install Python MySQL Client to work with MySQL database. Django Project to upload, view, delete file using ModelForm Setup Django project Create projectĬreate Django project named DjangoUploadModelForm with command:ĭjango-admin startproject DjangoUploadModelForm Install & Import Python MySQL Client – templates is the template for upload form and list view. – tutorials/forms.py defines ModelForm for Tutorial model. – tutorials/model.py defines Tutorial model with FileField and ImageField.

django file upload example

– tutorials/views.py defines functions to handle HTTP request.

django file upload example

– settings.py contains settings for media path, template & static folder directory.











Django file upload example