i want to create a mixin to be redirect user to a specified page if they're already authenticated. i want to be able to use this mixin in different parts of the application without having to rewrite the logic over and over again.
i get a accounts.views.view didn't return an HttpResponse object. It returned None instead. error if the user is not authenticated but it works if user is authenticated. accounts is the app_name
here's my code in mixin.py
class RedirectIfAuthenticatedMixin:
"""
RedirectIfAuthenticatedMixin: redirect authenticated user to different page via redirect_to parameter
"""
redirect_to = None
def get(self, request):
"""
Get request handler to check if user is already authenticated
then redirect user to specified url with redirect_to
"""
if request.user.is_authenticated:
return HttpResponseRedirect(self.get_redirect_url())
# return ??? <- WHAT TO WRITE HERE TO ALLOW REQUEST TO CONTINUE EXECUTION
def get_redirect_url(self):
"""
Get the specified redirect_to url
"""
if not self.redirect_to:
raise ImproperlyConfigured('no url to redirect to. please specify a redirect url')
return str(self.redirect_to)
it works when i add this to the view itself
class RegisterView(RedirectIfAuthenticatedMixin, FormView):
"""
RegisterView: form view to handle user registration
"""
template_name = 'registration/register.html'
success_url = reverse_lazy('accounts:activation-sent')
form_class = RegistrationForm
def form_valid(self, form):
"""
Method to handle form submission and validation
"""
# save user information
user = form.save(commit = False)
user.email = form.cleaned_data['email']
user.set_password(form.cleaned_data['password'])
user.is_active = False
user.save()
# email configuration/compose to send to user
current_site = get_current_site(self.request)
subject = 'Activate Your Padumba Account'
message = render_to_string('registration/account_activation_email.html', {
'user': user,
'domain': current_site.domain,
'uid': urlsafe_base64_encode(force_bytes(user.pk)),
'token': account_activation_token.make_token(user),
})
# send the account confirmation email to the user
user.email_user(subject = subject, message = message)
# send a flash message to the user
messages.success(self.request, ('Check Your Email For Account Activation Link'))
return super().form_valid(form)
def get(self, request, *args, **kwargs):
if request.user.is_authenticated:
return HttpResponseRedirect(reverse_lazy('accounts:index'))
return super(RegisterView, self).get(request, *args, **kwargs)