Search code examples
djangodjango-rest-frameworkdjango-sessions

Django - custom sessions with REST login support


We need to implement Django app with custom sessions. What we need: - login is being done via REST - Django is only wrapper for REST services

Can I use some king of custom sessions in django which will support login via REST? (lats assume I will send login and password via rest)

Could you suggest any built-in solution or 3rd party lib for such use case?


Solution

  • I think you don't need any kind of custom session in django and any 3rd party lib either. Django session app doesn't have strong (sql-)relations with other django-applications(auth in your case). If you want to do login via your REST-service, just write your login function and put result in session. Something like this:

    def login(login, pass):
      return urllib.urlget('https://mysite.io/login?user=%s&pass=%s'%(login, pass))
    
    def my_veiew(request):
      if request.method=='POST':
        request.session['user']=login(request.POST['user'], request.POST['pass'])
        return HttpResponseRedirrect('somewhere')
      return TemplateResponse(request, 'template.html', {'form': form})
    

    Note that you can put any type of python object in your session, for example your own wrapper class. This is why I like django session so much! =)

    You can find more information about django session here and choose which type of session backend fit for your project.

    Cheers!