Experiment-11 Full Stack Django Search Creators
Experiment-11 Full Stack Django Search Creators
Experiment-11
Develop example Django app that performs CSV and PDF generation for any models
created in previous laboratory component.
Step-01: This app will be created in the Django project we made earlier.
Locate the INSTALLED_APPS list and add the name of your new app to the list:
• Open the models.py file inside the student_course_registration_app and define your
models:
class Book(models.Model):
title = models.CharField(max_length=200)
author = models.CharField(max_length=200)
publication_date = models.DateField()
def __str__(self):
return self.title
Step-04: Create a views.py file in your books app and define the views for generating CSV
and PDF files
import csv
def export_books_csv(request):
response = HttpResponse(content_type='text/csv')
writer = csv.writer(response)
writer.writerow(book)
return response
def export_books_pdf(request):
books = Book.objects.all()
template_path = 'book_list.html'
response = HttpResponse(content_type='application/pdf')
template = get_template(template_path)
html = template.render(context)
pisa_status = pisa.CreatePDF(
html, dest=response)
if pisa_status.err:
return response
Step-04: In your books app, create a templates directory and an html file for rendering the
PDF:
books/
templates/
books/
book_list.html
book_list.html
<!DOCTYPE html>
<html>
<head>
<title>Book List</title>
<style>
body {
table {
border-collapse: collapse;
width: 100%;
th, td {
padding: 8px;
text-align: left;
</style>
</head>
<body>
<h1>Book List</h1>
<table>
<thead>
<tr>
<th>Title</th>
<th>Author</th>
<th>Publication Date</th>
</tr>
</thead>
<tbody>
<tr>
</tr>
{% endfor %}
</tbody>
</table>
</body>
</html>
urlpatterns = [
Step-07: Create the book titles and author and publication date
Django Shell
• If you just want to create some sample data for testing or development purposes, you can
run the code in the Django shell as shown in the previous example. This is a quick and
easy way to create objects interactively.
Data Migration
• If you want to include the book data as part of your project's initial data setup, you can
create a data migration. Here's how you can do it
• This will create a new empty migration file in the books/migrations/ directory.
• Open the newly created migration file (e.g., books/migrations/0002_auto_<...>.py) and
add the code to create the book objects in the operations list of the Migration class.
Book.objects.bulk_create([
])
class Migration(migrations.Migration):
dependencies = [
('books', '0001_initial'),
operations = [
migrations.RunPython(create_book_data),
• If you need to create the book data programmatically (e.g., during deployment or as part
of a data import process), you can create a custom Python script and run it as needed.
• Create a new Python file (e.g., create_book_data.py) in your project's root directory, and
add the code to create the book objects:
import os
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'fullstack_project.settings')
import django
django.setup()
Book.objects.bulk_create([
])
python create_book_data.py
• Open the urls.py file inside your project and include the URLs from the book app:
• In the VS Code terminal, navigate to your Django project's directory (the one containing
manage.py).
• Run the development server