0% found this document useful (0 votes)
14 views5 pages

CSV and PDF

Uploaded by

mshreyas817
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)
14 views5 pages

CSV and PDF

Uploaded by

mshreyas817
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/ 5

Develop a Model form for student that contains his topic chosen for

project, languages used and duration with a model called project.


Develop example Django app that performs CSV and PDF generation
from the above example.

1. django-admin startproject student_project


2. cd student_project
3. python manage.py startapp projects
4. student_project/settings.py

INSTALLED_APPS = [

...

'projects',

5. projects/models.py:

from django.db import models

class Project(models.Model):

student_name = models.CharField(max_length=100)

topic = models.CharField(max_length=200)

languages_used = models.CharField(max_length=200)

duration = models.CharField(max_length=50)

def __str__(self):

return self.topic

In CMD:
6. python manage.py makemigrations
7. python manage.py migrate

8. projects/forms.py:

from django import forms


from .models import Project

class ProjectForm(forms.ModelForm):

class Meta:

model = Project

fields = ['student_name', 'topic', 'languages_used', 'duration']

9. projects/views.py

from django.shortcuts import render, redirect

from .forms import ProjectForm

from .models import Project

import csv

from django.http import HttpResponse

from reportlab.pdfgen import canvas

def project_create_view(request):

if request.method == 'POST':

form = ProjectForm(request.POST)

if form.is_valid():

form.save()

return redirect('project_list')

else:

form = ProjectForm()

return render(request, 'projects/project_form.html', {'form': form})

def project_list_view(request):

projects = Project.objects.all()

return render(request, 'projects/project_list.html', {'projects': projects})

def export_projects_csv(request):

response = HttpResponse(content_type='text/csv')
response['Content-Disposition'] = 'attachment; filename="projects.csv"'

writer = csv.writer(response)

writer.writerow(['Student Name', 'Topic', 'Languages Used', 'Duration'])

projects = Project.objects.all()

for project in projects:

writer.writerow([project.student_name, project.topic, project.languages_used,


project.duration])

return response

def export_projects_pdf(request):

response = HttpResponse(content_type='application/pdf')

response['Content-Disposition'] = 'attachment; filename="projects.pdf"'

p = canvas.Canvas(response)

p.drawString(100, 800, "Project List")

projects = Project.objects.all()

y = 750

for project in projects:

p.drawString(100, y, f"Student Name: {project.student_name}")

p.drawString(100, y-15, f"Topic: {project.topic}")

p.drawString(100, y-30, f"Languages Used: {project.languages_used}")

p.drawString(100, y-45, f"Duration: {project.duration}")

y -= 60

p.showPage()

p.save()
return response

10. projects/urls.py

from django.urls import path

from . import views

urlpatterns = [

path('new/', views.project_create_view, name='project_create'),

path('', views.project_list_view, name='project_list'),

path('export/csv/', views.export_projects_csv, name='export_projects_csv'),

path('export/pdf/', views.export_projects_pdf, name='export_projects_pdf'),

11. student_project/urls.py

from django.contrib import admin

from django.urls import path, include

urlpatterns = [

path('admin/', admin.site.urls),

path('projects/', include('projects.urls')),

12. projects/templates/projects

project_form.html:

<!-- projects/templates/projects/project_form.html -->

<h2>New Project</h2>

<form method="post">

{% csrf_token %}

{{ form.as_p }}

<button type="submit">Save</button>
</form>

project_list.html:

<!-- projects/templates/projects/project_list.html -->

<h2>Project List</h2>

<table>

<tr>

<th>Student Name</th>

<th>Topic</th>

<th>Languages Used</th>

<th>Duration</th>

</tr>

{% for project in projects %}

<tr>

<td>{{ project.student_name }}</td>

<td>{{ project.topic }}</td>

<td>{{ project.languages_used }}</td>

<td>{{ project.duration }}</td>

</tr>

{% endfor %}

</table>

<a href="{% url 'export_projects_csv' %}">Export CSV</a>

<a href="{% url 'export_projects_pdf' %}">Export PDF</a>

13. python manage.py runserver

http://127.0.0.1:8000/projects/

You might also like