Now, as the workshop_registration admin page has been created, we can start creating the url and corresponding function for allowing the user to register for a particular workshop.
In the event/views.py
file :
from django.shortcuts import render, get_object_or_404, redirect
from django.contrib.auth.decorators import login_required
from .models import Workshop, WorkshopGallery, Workshopregistration
@login_required(login_url='login')
def register_workshop(request, workshop_slug):
if request.method=="POST":
workshop = get_object_or_404(Workshop, slug=workshop_slug)
try:
Workshopregistration.objects.create(student=request.user, workshop=workshop)
except:
pass
return redirect('workshop.detail', slug=workshop_slug)
Here, the user would only be registered for a particular workshop if the request method is POST. Next, the user is registered for the workshop inside a try except block. If the user has already registered, then it would throw an error. The error can be further handled and some action can be taken based on that. After that, it would redirect back to the workshop_detail page.
After doing this, the url should also be included in event/urls.py
file.
path('workshop/<slug:workshop_slug>/register/', views.register_workshop, name='register_workshop'),
Now, the url and function are ready, this can now be included as a form in the workshop_detail page to handle the registration process.