1from django.contrib.auth import authenticate, login
2
3def my_view(request):
4 username = request.POST['username']
5 password = request.POST['password']
6 user = authenticate(request, username=username, password=password)
7 if user is not None:
8 login(request, user)
9 # Redirect to a success page.
10 ...
11 else:
12 # Return an 'invalid login' error message.
13 ...
14
1from django.contrib.auth.mixins import LoginRequiredMixin
2
3LOGIN_URL = 'your_url'
1from django.contrib.auth.mixins import LoginRequiredMixin
2
3class MyView(LoginRequiredMixin, View):
4 login_url = '/login/'
5 redirect_field_name = 'redirect_to'
6