Develop a Django app that performs PDF generation for any models created in previous laboratory components
Programs:-
views.py:-
from django.http import HttpResponse from ap3.models import Course from reportlab.pdfgen import canvas def construct_pdf_from_model(request): courses = Course.objects.all() response = HttpResponse(content_type='application/pdf') response['Content-Disposition'] = 'attachment; filename="courses_data.pdf"' c = canvas.Canvas(response) c.drawString(70, 720, "Course Name") c.drawString(170, 720, "Course Code") c.drawString(270, 720, "Credits") y = 660 for course in courses: c.drawString(70, y, course.course_name) c.drawString(170, y, course.course_code) c.drawString(270, y, str(course.course_credits)) y = y - 20 # Adjust this value based on your preference for spacing between rows c.showPage() c.save() return response
urls.py:-
from django.contrib import admin from django.urls import path from ap3.views import construct_pdf_from_model admin.site.site_header = "My Site Header" admin.site.site_title = "My Site Title" admin.site.index_title = "My Site Index" urlpatterns = [ path('secretadmin/', admin.site.urls), path('construct_pdf_from_model/', construct_pdf_from_model), ]