I found such a piece of code, but I don't understand how it works. And if you set the path to another page without performing authorization - this page opens.
@login_required
def my_view(login_url='/'):
return HttpResponseRedirect('/config')
I need your help.
(how authorization is performed)
class LoginView(View):
def get(self, request, *args, **kwargs):
form = LoginForm(request.POST or None)
context = {
'form': form
}
return render(request, 'manager/login.html', context)
def post(self, request, *args, **kwargs):
form = LoginForm(request.POST or None)
context = {
'form': form
}
if form.is_valid():
username = form.cleaned_data['username']
password = form.cleaned_data['password']
user = authenticate(username=username, password=password)
if user:
login(request, user)
return HttpResponseRedirect('/config')
return render(request, 'manager/login.html', context)
You want to redirect all unauthenticated users to your login page until they login ?
I think using MiddleWares is best way to handle this because you want to do it on all of your views.
And in your settings.py you need to add your MiddleWare: