Prerequisites:
Before practicing the script of this tutorial, you have to complete the following tasks.
- Install the Django version 3+ on Ubuntu 20+ (preferably)
- Create a Django project
- Run the Django server to check the server is working correctly or not.
Setup a Django App:
Run the following command to create a Django app named downloadapp. $ python3 manage.py startapp downloadapp
Run the following command to create the user for accessing the Django database. If you have created the user before then, you don't need to run the command.
$ python3 manage.py createsuperuserAdd the app name in the INSTALLED_APP part of the settings.py file.
INSTALLED_APPS = […
'downloadapp'
]
Create a folder named templates inside the downloadapp folder and set the template's location of the app in the TEMPLATES part of the settings.py file.
TEMPLATES = [… .
'DIRS': ['/home/fahmida/django_pro/dopwnloadapp/templates'],
… .
,
]
Download a Text File:
Modify the views.py file that is inside the downloadapp folder with the following script. The download_file() function will download a text file named text.txt for a particular URL path. The file has opened for reading at the beginning of the script. The mime type and header information have been set to download the file.
views.py
# Import mimetypes moduleimport mimetypes
# import os module
import os
# Import HttpResponse module
from django.http.response import HttpResponse
def download_file(request):
# Define Django project base directory
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
# Define text file name
filename = 'test.txt'
# Define the full file path
filepath = BASE_DIR + '/downloadapp/Files/' + filename
# Open the file for reading content
path = open(filepath, 'r')
# Set the mime type
mime_type, _ = mimetypes.guess_type(filepath)
# Set the return value of the HttpResponse
response = HttpResponse(path, content_type=mime_type)
# Set the HTTP header for sending to browser
response['Content-Disposition'] = "attachment; filename=%s" % filename
# Return the response value
return response
Now, open the urls.py file from the Django project and update the file with the following script. The text file will download if the path, 'download/' is used after the base URL.
urls.py
# Import path modulefrom django.urls import path
# Import views
from downloadapp import views
# Set path for download
urlpatterns = [
path('download/', views.download_file),
]
Open the browser and execute the following URL that will open the download dialog box for the user.
http://localhost:8000/download
The file will be downloaded if the user clicks on the OK button after selecting the Save File option.
Download PDF File Using the Template:
A template will be required to create if you want to add the download option with the download link. Create an HTML file named file.html with the following script to display the download link in the browser to download a PDF file. According to the hyperlink that is used in the script will download the CF.pdf file.
file.html
Download File using Django
Download PDF