1

Hello to Stack Overflow community, I had created login and logout functions in django views.py hence i had successfully achieved login and logout methods also but i am confusing know that how can i pass data of this logged in user details to my class based views in views.py because i want to give access to my class based views only if user login happened

views.py

def admin_login(request):
    context = {}
    if request.method == 'POST':
        username = request.POST['username']
        password = request.POST['password']
        user = authenticate(request, username=username, password=password)
        if user:
            login(request, user)
            context['user'] = request.user
            return redirect('profile')
        else:
            context['error'] = 'Provide Valid Credentials'
            return render(request, "secret_template.html", context)
    else:
        return render(request, "secret_template.html", context)


def admin_logout(request):
        logout(request)
        return redirect('secretview')

i want to authenticate below view only if user logged in

class index(TemplateView):
    template_name = 'secret_template.html'

2 Answers 2

4

Use the LoginRequiredMixin in your view.

from django.contrib.auth.mixins import LoginRequiredMixin

class index(LoginRequiredMixin, TemplateView):
    login_url = reverse_lazy('admin_login') # or whatever
    template_name = 'aapp/index.html'
Sign up to request clarification or add additional context in comments.

4 Comments

Dear Daniel it worked thanks for guiding me, but how can i get this current user deatils to my index view with this login....?
I don't understand what you mean. What do you want to do with them? Have you read any of the docs on using the auth framework?
Daniel Roseman thanks no problem i will go through the docs where i am not clear but thnaks for teaching me
@naniraju, you need the request field in your View object. in any method, for example get_context, access self.request.user as you need to.
1

Following Django documentation you'll find some generic examples fitting your request: For function based views you can simply use the login_required decorator.

from django.contrib.auth.decorators import login_required

@login_required
def my_view(request):
    return Something

For class based views you have an example with method_decorator

from django.contrib.auth.decorators import login_required
from django.utils.decorators import method_decorator

@method_decorator(login_required, name='dispatch')
class ProtectedView(TemplateView):
template_name = 'secret.html'

edit: I cannot comment, so I add this here: You can handle a user instance from request.user inside your views methods.

1 Comment

important to note that this decorator does not work on class based views, and one should use the solution stated above

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.