[Django]-I would like to add condition to Django DetailView

8๐Ÿ‘

โœ…

I would use the PermissionRequiredMixin. With this you can specify specific permissions users need to have or override the has_permission method.

from django.contrib.auth.mixins import PermissionRequiredMixin

class RecruitView(PermissionRequiredMixin, generic.DetailView):
    ...

    login_url = '/login/'
    permission_denied_message = 'you did not confirmed yet. please check your email.'

    def has_permission(self):
         return self.request.user.email_confirmed

This will redirect users without the email_confirmed to the login_url where you can display the error message. In order to use the index.html template instead you might need to override the handle_no_permission method.

๐Ÿ‘คikkuh

Leave a comment