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

Linking Department with HOD

In order to link HOD with particular department, lets create a new model named HodProfile to store the hod's department as well as some other information.

Create a model in authentication/models.py file.

class HodProfile(models.Model):
    hod = models.OneToOneField(User, on_delete=models.CASCADE)
    department = models.ForeignKey(Department, on_delete=models.CASCADE)
    office_number = models.CharField(max_length=25)
    faculty_code = models.CharField(max_length=10)

    def __str__(self):
        return f"{self.hod}"

This stores the hod's detail along with the User model with a one-to-one field, and the department as a foreign key mapped with Hod. Along with this information, the office number and faculty code are taken.

Now, you can run the migrations to make changes in the database.

python manage.py makemigrations
python manage.py migrate

alt text This creates another table and we can continue to register this model in admin page. Inside authentication/admin.py :

@admin.register(HodProfile)
class HodProfileAdmin(admin.ModelAdmin):
    list_display = ('hod', 'department', 'office_number', 'faculty_code')
    search_fields = ('office_number', 'faculty_code')
    list_filter = ('department',)

Now HodProfiles can be created by admin through the admin page in Django.

alt text alt textClicking on the save button will generate a faculty profile, for the particular user.

alt text Here, while addition of HodProfile, for now only 2 users are there so its easier to select through dropdown. alt text But as the number of Users increases, this list will also increase. At that point it will be difficult to create particular Hod Profiles. In that case another variable may be added inside the admin class autocomplete_fields, This will allow a search option to easily select the user.

@admin.register(HodProfile)
class HodProfileAdmin(admin.ModelAdmin):
    list_display = ('hod', 'department', 'office_number', 'faculty_code')
    search_fields = ('office_number', 'faculty_code')
    list_filter = ('department',)
    autocomplete_fields = ('hod', 'department')

After doing this change, the admin site will be like this. alt text This make it easier for selecting users. Further in this we will see how hod's or admin can add events.