Django

College Event Registration Website

CampusConnect Introduction and Setup Configuring settings file for template, static and media files Setting up Tailwind Creating Custom User Model Creating Super User for CampusConnect Registering Custom User Model Search and Filter for User Model Serving Media Files During Development Categorizing Departments Linking Department with HOD Creating Abstract Model for Event Creating Model for Workshop Customizing Admin Page for Workshop Update in Model AbstractEvent Adding Instructor for Workshop Instructor Model Admin Page Adding Poster Field in Abstract Event Providing Access to HOD Access Update for HOD Restricting HOD Access to Particular Department AbstractEvent On Spot Registration Field Creating Workshop Object Creating and Linking Home Page Displaying Workshop on Home Page Styling Home Page Adding Workshop Detail Page Link Workshop Detail Page Workshop Detail Page Styling Workshop Instructor Details Workshop Detail Contact Contact Admin Page Many to Many Field for Contact Displaying Contact on Workshop Detail Page Adding Title for Workshop Detail Page Adding Gallery for Workshop Workshop Gallery Admin Page Displaying Gallery Images on Website Through Context Displaying Gallery Images on Website through template tags Authentication for users User Registration User Registration Submission Logout Functionality For User Login Functionality for User Model For Workshop Registration Workshop Registration Admin Page Register Workshop Function Register Button in Workshop Page Validations Before Workshop Registration Workshop Registration Closed Validaiton User Already Registered for Workshop Validation Workshop Registration Report From Admin Page Export using Library in Django Admin Extending Abstract Event for Hackathons

Workshop Registration Report From Admin Page

The students those who have registered for a particular workshop can be viewed through the admin page. We would also like to take a report of them, that is in the form of a csv file or pdf file. This can be further used for printing the details for manual attendance, or for any record purposes.

To do this, we can add an action inside the WorkshopRegistrationAdmin class, to download the selected rows as a csv file.

In order to this, these changes needs to be done inside the event/admin.py file :

import csv
from django.http import HttpResponse
from django.utils.text import slugify

@admin.register(Workshopregistration)
class WorkshopRegistrationAdmin(admin.ModelAdmin):
    list_display = ('workshop', 'student', 'registered_date')
    search_fields = ('workshop__name', 'student__email')
    list_filter = ('registered_date', 'workshop')
    autocomplete_fields = ('workshop', 'student')

    def get_queryset(self, request):
        qs = super().get_queryset(request)
        if not request.user.is_superuser:
            qs=qs.filter(workshop__department = request.user.hodprofile.department)
        return qs
    
    def export_as_csv(self, request, queryse1t, queryset):
        response = HttpResponse(content_type='text/csv')
        response['Content-Disposition'] = f'attachment; filename={slugify(queryset.model.__name__)}-export.csv'

        writer = csv.writer(response)
        fields = [field.name for field in queryset.model._meta.fields]
        writer.writerow(fields)

        for obj in queryset:
            row = [getattr(obj, field) for field in fields]
            writer.writerow(row)

        return response
    export_as_csv.short_description = "Export as CSV"

    def get_actions(self, request):
        actions = super().get_actions(request)
        actions['export_as_csv'] = (self.export_as_csv, 'export_as_csv', "Export selected %(verbose_name_plural)s as CSV")
        return actions

The above functions takes all the columns that are present in a particular model and allows it to download as a csv file. So after doing this, you could see the same on the admin page.

alt text Other than delete, there is another option to export as csv. When you click on go, it would download a file which would have the following content.

id workshop student registered_date
2 CyberGuard mustafa@edkool.com 2023-12-12 06:45:30.140151+00:00
1 Chain Craft mustafa@edkool.com 2023-12-06 03:06:26.584382+00:00

The csv file would contain these data.

The problem here is, we are not getting all the data that we would want in a csv file. Like, we may require some more details such as student name, his email, contact number, and then coming to workshop say we may want to include the venue as well. So in this case we can add the fields that we want and select the particular row values for it.

@admin.register(Workshopregistration)
class WorkshopRegistrationAdmin(admin.ModelAdmin):
    list_display = ('workshop', 'student', 'registered_date')
    search_fields = ('workshop__name', 'student__email')
    list_filter = ('registered_date', 'workshop')
    autocomplete_fields = ('workshop', 'student')

    def get_queryset(self, request):
        qs = super().get_queryset(request)
        if not request.user.is_superuser:
            qs=qs.filter(workshop__department = request.user.hodprofile.department)
        return qs
    
    def export_as_csv(self, request, queryse1t, queryset):
        response = HttpResponse(content_type='text/csv')
        response['Content-Disposition'] = f'attachment; filename={slugify(queryset.model.__name__)}-export.csv'

        writer = csv.writer(response)
        fields = ['Workshop', 'Venue', 'Student Name', 'Student Email', 'Student Contact']
        writer.writerow(fields)

        for obj in queryset:
            row = [obj.workshop.workshop_title, obj.workshop.venue, obj.student.first_name, obj.student.email, obj.student.mobile_number]
            writer.writerow(row)

        return response
    export_as_csv.short_description = "Export as CSV"

    def get_actions(self, request):
        actions = super().get_actions(request)
        actions['export_as_csv'] = (self.export_as_csv, 'export_as_csv', "Export selected %(verbose_name_plural)s as CSV")
        return actions

So here, we have included the custom fields and the corresponding values for it. You can feel free to edit the fields and insert values as needed.

Now, if you click on export as csv, it would generate a result like this. alt text

Workshop Venue Student Name Student Email Student Contact
Cyber Secuirty Fundamentals Sapphire Auditorium Mustafa mustafa@edkool.com 1234567890
Block Chain Technology Main Seminar Hall Mustafa mustafa@edkool.com 1234567890

So this way, custom fields as well can be added inside the export action.