0% found this document useful (0 votes)
21 views3 pages

PGM 11 Execfinal

abcd

Uploaded by

Rutuja K
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views3 pages

PGM 11 Execfinal

abcd

Uploaded by

Rutuja K
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

11.

Develop example Django application that performs CSV generation for any models
created in previous laboratory component.

1. Create and Activate Virtual Environment


apython -m venv myenv
myenv\Scripts\activate # For Windows
# or
source myenv/bin/activate # For macOS/Linux

#### 2. Install Django and ReportLab


pip install django reportlab

#### 3. Create Django Project


django-admin startproject myproject
cd myproject

#### 4. Create Django App


python manage.py startapp ap3

#### 5. Update `settings.py`


Add the `ap3` app to `INSTALLED_APPS`:
# myproject/settings.py
INSTALLED_APPS = [
...
'ap3',
...
]
```
#### 6. Define Models in `models.py`
Create your models in `ap3/models.py`:
# ap3/models.py
from django.db import models
class Course(models.Model):
course_name = models.CharField(max_length=100)
course_code = models.CharField(max_length=10)
course_credits = models.IntegerField()
def __str__(self):
return self.course_name

#### 7. Create and Apply Migrations


python manage.py makemigrations ap3
python manage.py migrate

#### 8. Update Views in `views.py`


Create the views for CSV and PDF generation in `ap3/views.py`:
# ap3/views.py

from django.http import HttpResponse


from .models import Course
import csv
from reportlab.pdfgen import canvas
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

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 -= 60
c.showPage()
c.save()
return response

9. Update URLs in `urls.py`


Update your project's URL configuration in `myproject/urls.py`:
# myproject/urls.py
from django.contrib import admin
from django.urls import path
from ap3.views import construct_csv_from_model, construct_pdf_from_model
urlpatterns = [
path('admin/', admin.site.urls),
path('construct_csv/', construct_csv_from_model),
path('construct_pdf/', construct_pdf_from_model),
]

10. Run the Development Server


Start the development server:
python manage.py runserver
#### 11. Access the CSV and PDF Generation
Open your web browser and go to the following URLs to test the CSV and PDF generation:
- CSV Generation: `http://127.0.0.1:8000/construct_csv/`
- PDF Generation: `http://127.0.0.1:8000/construct_pdf/`

You might also like