I have a django project, where i download the template as a pdf using wkhtmltopdf.The pdf is downloaded when i click the download button, However when i open the pdf it says, “Error Failed to load PDF document.” and i cant open the downloaded pdf. Upon looking at the terminal i see this error: UserWarning: A {% csrf_token %} was used in a template, but the context did not provide the value. This is usually caused by not using RequestContext. What is causing this issue and how can it be solved?
this is my views.py:
@login_required def download_resume(request): # Retrieve data from your Django models personal_info = personalinfo.objects.filter(user=request.user).last() summaries = summary.objects.filter(user=request.user).last() experiences = experience.objects.filter(user=request.user) educations = education.objects.filter(user=request.user) certs = certificates.objects.filter(user=request.user) skillset = skills.objects.filter(user=request.user)
# Generate the PDF content using a template
template = get_template('template0.html')
context = {
'personal_info': personal_info,
'summaries': summaries,
'experiences': experiences,
'educations': educations,
'certs': certs,
'skillset': skillset
}
# Render the template with the context data
html_content = template.render(context)
# Configure pdfkit with the path to wkhtmltopdf
pdfkit_config = pdfkit.configuration(wkhtmltopdf='C:/Users/lulu/PProjects/resumebuilderproject/wkhtmltopdf/bin/wkhtmltopdf.exe')
# Convert HTML to PDF using wkhtmltopdf
pdf = pdfkit.from_string(html_content, False, configuration=pdfkit_config)
# Create a response with the PDF content
response = FileResponse(pdf, content_type='application/pdf')
response['Content-Disposition'] = 'attachment; filename="resume.pdf"'
return response
this is my html download button:
<form method="post" action="{% url 'download_resume' %}"> {%
csrf_token %} <button type="submit" class="download-button">
<i class="fas fa-download"></i> DOWNLOAD </button> </form>