djangodjango-authenticationdjango-registration

django-registration: how do I check whether the user is logged in before displaying a page


I followed this page to set up a django registration site. It's pretty awesome, and registration and authentication are nicely wrapped.

But, it doesn't show me, how do I check if a user is logged in, who this user is, before displaying a webpage? and how do I direct the user to a new page after logged in?

Thanks!


Solution

  • In a view, you can use if request.user.is_authenticated(): and the variable for the current user is request.user

    In a template, you can use {% if user.is_authenticated %} and the variable for the current user is user

    For redirecting a user after logging in, you can set up LOGIN_REDIRECT_URL variable in settings.py