I have built a Django app and it worked well. When I had run an another django app in a port(localhost:8000) and tried the app I built on the port, it says error like this.
AttributeError: 'AnonymousUser' object has no attribute 'profile'
Here is my code:
class DashboardView(auth_views.LoginView):
template_name = "dashboard/home.html"
verify_email_required = 'registration/verify_email_required.html'
form_class = ProfiledAuthenticationForm
def get(self, request, *args, **kwargs):
# context = {'form': self.form_class()}
context = self.get_context_data()
context['form'] = self.form_class()
if request.user.profile.is_verified:
return render(request, self.template_name, context)
else:
return render(request, self.verify_email_required, context)
Note: Initial when I built the app, it worked well, but for now it takes error. when user does not log in, the homepage was redirected to login page.
When I fixed like this, it runs well.
class DashboardView(auth_views.LoginView):
template_name = "dashboard/home.html"
verify_email_required = 'registration/verify_email_required.html'
form_class = ProfiledAuthenticationForm
def get(self, request, *args, **kwargs):
# context = {'form': self.form_class()}
context = self.get_context_data()
context['form'] = self.form_class()
if self.request.user.is_authenticated:
if request.user.profile.is_verified:
return render(request, self.template_name, context)
return render(request, self.verify_email_required, context)
return render(request, self.template_name, context)