I am trying to set up a django button so I can download a file. I tried following along a tutorial but I think i'm missing something. The urls.py
code below is the urls.py
file that i generated in the app section of my project. I am not sure if that is the correct one to be using.
The code below is not working, local host doesn't load the page. however when i comment out url('download_my_pdf', download_pdf),
in my urls.py
the site loads as we dont have that link back to the code in the views.
INDEX.HTML:
<input type="button" value="Download" onclick="window.open('download_my_pdf')">
VIEWS.PY:
from django.shortcuts import render
from django.http import HttpResponse
from wsgiref.util import FileWrapper
def index(request):
return render(request, "index.html", {})
def home(request):
return render(request, "home.html", {})
def download_pdf(request):
filename = 'faults.pdf'
content = FileWrapper(filename)
response = HttpResponse(content, content_type='application/pdf')
response['Content-Length'] = os.path.getsize(filename)
response['Content-Disposition'] = 'attachment; filename=%s' % 'faults.pdf'
return response
URLS.PY
from django.urls import url
from django.urls import path
from . import views
urlpatterns = [
path('', views.index, name="index"),
path('home', views.home, name="home"),
url('download_my_pdf', download_pdf),
]