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

Downloading Notes as CSV file

The list thats being displayed can be downloaded as a CSV file by modifying the ModelAdmin code.

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

@admin.register(Note)
class NoteAdmin(admin.ModelAdmin):
    list_display = ('pk','title', 'description')
    search_fields = ('title', 'description')
    list_editable = ('title','description',)

    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

This export_csv function takes all the fields that are present in the model and then writes those in a CSV file. After creating a function to download the data, we must add it in the actions dropdown in our admin page, which can be done by modifying the get_actions and linking it with our function.

After the code, is added there would be an extra option in the actions dropdown in the admin page, which will allow the user to download the selected notes as a CSV file.

alt text On clicking the go button, the selected notes data would be downloaded as a CSV file. alt text Here, all the fields have been included in the csv file, you can further restrict the fields by modifying the export_csv function to include only the field names that you want.

For example,

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 = ['id', 'title']
        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"

The above code will download the CSV file with only the id and title fields. alt textSimilarly, fields may be modified the way you want, when you don't want some sensitive information to get downloaded as CSV file such as passwords.