0
votes

I have a model models.py

class Dibbs(models.Model):
     name = models.CharField(max_length=20) 

views.py

def index(request):
    dibbs_obj = Dibbs.objects.all()
    solit_list = Dibbs.objects.values_list('name', flat=True).distinct()
    context ={'dibbs_obj':dibbs_obj,'solit_list':solit_list}
    return render(request,"Partsbase-data-table (1).html",context)

def display_pdf(request):
    r = request.GET.get('solit_list')
    buffer = io.BytesIO()
    buffer.seek(0)
    return FileResponse(buffer, as_attachment=True, filename='')

template:

{% for i in dibbs_obj %}
<td data-label="name"><a href="{% url 'display_pdf' %}">{{ i.name }}</a></td>
{% endfor %}

There is the exact pdf file of name in the media/uploads/ folder.

So, I created another view function to stream the pdf file as file response when I click on the name in the template

How to properly use this FileResponse function so that it opens the link of the same name?

(In this case, the data that is in the name have its corresponding pdf file as name.pdf in the media/uploads/ folder)