Develop a Django app that performs CSV generation for any models created in previous laboratory components
Program:-
views.py:-
from django.http import HttpResponse from ap3.models import Course import csv def construct_csv_from_model(request): courses = Course.objects.all() response = HttpResponse(content_type='text/csv') response['Content-Disposition'] = 'attachment; filename="courses_data.csv"' writer = csv.writer(response) writer.writerow(['Course Name', 'Course Code', 'Credits']) for course in courses: writer.writerow([course.course_name, course.course_code, course.course_credits]) return response
urls.py:-
from django.contrib import admin from django.urls import path, re_path from ap3.views import construct_csv, construct_csv_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_csv/', construct_csv), path('construct_csv_from_model/', construct_csv_from_model), ]