As a part of our Django tutorial series, we have learned about setting a static file directory in Django. In this tutorial, we are going to learn how to use and load static files in Django.
Hard coding the static file path for any file in Django is not good programming practice. Static file path can be changed in the future and if you are hard coding the static file path, your program will fail.
In this tutorial, we are going to learn how you can access any static file path programmatically. Instead of hardcoding we are going to get the static file path dynamically at run time.
Let’s we are going to access the static file in views.py. (You can access the static file path in any Python script.)
Static files can be images, pdf, CSS, JavaScript, etc… In this example, we are trying to access a pdf file.
Open views.py. We want to get the static path in the sample_view()
method.
Use static()
from django.templatetags.static
module.
from django.templatetags.static import static def sample_view(request): filepath = static('sample.pdf')
Note: If you are using open()
method to read the static file, static()
method is not going to work and so is the Django static file.
from django.templatetags.static import static def sample_view(request): filepath = static('sample.pdf') return FileResponse( open(filepath, 'rb'), content_type='application/pdf' )
If you open the URL for this view, you will get an exception as FileNotFoundError
.
FileNotFoundError at /sample [Errno 2] No such file or directory: '/static/sample.pdf' Request Method: GET Request URL: http://127.0.0.1:8000/sample Django Version: 3.1.3 Exception Type: FileNotFoundError Exception Value: [Errno 2] No such file or directory: '/static/sample.pdf'
In this case, you need to use join()
method from os.path
module.
import os from django.http import HttpResponse def sample_view(request): filepath = os.path.join('static', 'sample.pdf') return FileResponse( open(filepath, 'rb'), content_type='application/pdf' )
In the above example, we are reading the pdf file and rendering it using FileResponse()
method. Make sure, you are setting “content_type” as “application/pdf”.
Somtimes, you also have to use decode() method, if the static file has different encoding.
import os from django.http import HttpResponse def sample_view(request): filepath = os.path.join('static', 'sample.pdf') with open(filepath, 'rb') as pdf: response = HttpResponse( pdf.read().decode(), content_type='application/pdf') response['Content-Disposition'] = 'inline;filename=sample.pdf' return response
This way, you have multiple options to access static files in Django view. Let’s see reading static file path in the Django template.
Accessing static files in HTML templates is pretty easy.
Just two lines of code.
{% load static %}
{% static 'sample.pdf' %}
This will simply print the pdf file path.
You can use this file path in src
attribute, to show the image (using img
tag) or to display pdf file (using iframe
tag). You should know the basic HTML tags.
{% load static %} <iframe frameborder="1" class="responsive-iframe" src="{% static 'sample.pdf' %}" ></iframe>
Displaying image in Django webpage.
{% load static %} <img src="{% static 'pricing.svg' %}" alt="sample image" />
This is all about this tutorial to access and load static files in Django. If you have any queries, let me know in the comment section.