Skip to content

Commit 5fe4505

Browse files
Fix PDF download error for documents without PDF files (#358)
Fixes DOCHUB-1V7 Co-authored-by: Claude Sonnet 4.5 <noreply@anthropic.com>
1 parent e95ffcc commit 5fe4505

2 files changed

Lines changed: 12 additions & 2 deletions

File tree

catalog/templates/catalog/course.html

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,8 @@ <h1 class="d-flex align-items-center gap-2">
216216
</svg>
217217
</a>
218218
{% endif %}
219-
<a class="btn" title="Télécharger" href="{% url 'document_pdf' document.pk %}"
219+
<a class="btn" title="Télécharger"
220+
href="{% if document.pdf and document.pdf.name %}{% url 'document_pdf' document.pk %}{% else %}{% url 'document_original' document.pk %}{% endif %}"
220221
data-turbo="false">
221222
<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor"
222223
class="bi bi-download" viewBox="0 0 16 16">

documents/views.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
from django.contrib import messages
66
from django.contrib.auth.decorators import login_required
77
from django.db.models import F
8-
from django.http import HttpResponse, HttpResponseRedirect
8+
from django.http import Http404, HttpResponse, HttpResponseRedirect
99
from django.shortcuts import get_object_or_404, redirect, render
1010
from django.urls import reverse
1111
from django.views.decorators.clickjacking import xframe_options_sameorigin
@@ -254,6 +254,10 @@ def document_vote(request, pk):
254254
def document_original_file(request, pk):
255255
document = get_object_or_404(Document, pk=pk)
256256

257+
# Check if original file exists
258+
if not document.original or not document.original.name:
259+
raise Http404("Le fichier original n'existe pas pour ce document")
260+
257261
body = document.original.read()
258262

259263
response = HttpResponse(body, content_type="application/octet-stream")
@@ -274,6 +278,11 @@ def document_original_file(request, pk):
274278
@login_required
275279
def document_pdf_file(request, pk):
276280
document = get_object_or_404(Document, pk=pk)
281+
282+
# Check if PDF file exists
283+
if not document.pdf or not document.pdf.name:
284+
raise Http404("Le fichier PDF n'existe pas pour ce document")
285+
277286
body = document.pdf.read()
278287

279288
response = HttpResponse(body, content_type="application/pdf")

0 commit comments

Comments
 (0)