Reputation: 9104
I want to display different thing at the same URL (home page) depending on whether a user is logged in or not.
So, if he is not authenticated I'll display a page which does not involve any DB query, otherwise, if he is logged in, I'll display his projects (this involves DB access).
So, how can I accomplish this, given that:
Upvotes: 1
Views: 67
Reputation: 7631
Generally one would use the Login Required decorator but since you only have a single URL you could check whether request.user.is_authenticated() is True. If it's True return the template corresponding to the logged in user, otherwise return the other one.
Class Based Views
Take a look at this: login required in TemplateView
It used the dispatch method to check for a user being authenticated in a class based view.
from django import http from django.views import generic
class AboutView(generic.TemplateView):
""" About page view. """
template_name = 'about.html'
def dispatch(self, request, *args, **kwargs):
if not request.user.is_authenticated():
raise http.Http404
return super(AboutView, self).dispatch(request, *args, **kwargs)
def get_context_data(self, **kwargs):
ctx = super(AboutView, self).get_context_data(**kwargs)
ctx['something_else'] = None # add something to ctx
return ctx
Upvotes: 0
Reputation: 4970
Check to see if User.is_authenticated(), if they are, query for the projects, if not don't query for the projects, and in the view see if the projects variable is set or not.
Upvotes: 1