django-cas-ng Example Project

An example project to demo how easy to integrate with django-cas-ng

django-cas-ng is Django CAS (Central Authentication Service) 1.0/2.0/3.0 client library to support SSO (Single Sign On) and SLO (Single Logout)!

To demo how easy to integrate with django-cas-ng, here is an example project to show how to use django-cas-ng to work with CAS login/logout.

The CAS login flow outline as below. The App Service here is refer to this demo project.

CAS example login flow

Example Project

Here is steps to create this example project:

Create a django project and install django-cas-ng

$ django-admin startproject mysite

$ cd mysite

$ pip install django-cas-ng
Collecting django-cas-ng
  Using cached
Installing collected packages: django-cas-ng
Successfully installed django-cas-ng-4.0.1

$ pip freeze | grep django-cas-ng >> requirements.txt

Config django-cas-ng


Config INSTALLED_APPS: Add django_cas_ng to INSTALLED_APPS:


Config MIDDLEWARE: Add django_cas_ng.middleware.CASMiddleware to MIDDLEWARE:




Config CAS server URL and CAS protocol version: To make this example project work, I created a demo CAS server host on Heroku. The server URL is



Add accounts/login and accounts/logout URL. To demo the login/logout, I also add a simple home page to show login/logout, the view is view.index.

from django.contrib import admin
from django.urls import path
import django_cas_ng.views
from . import views

urlpatterns = [
    path('', views.index, name='index'),
    path('accounts/login', django_cas_ng.views.LoginView.as_view(), name='cas_ng_login'),
    path('accounts/logout', django_cas_ng.views.LogoutView.as_view(), name='cas_ng_logout'),


Create a new file named mysite/ and implement index method.

The demo view will check user is authenticated by using request.user.is_authenticated. If it is authenticated, it will get logged user name from request.user.username

from django.http import HttpResponse

def index(request):
    if request.user.is_authenticated:
        return HttpResponse('<p>Welcome to <a href="">django-cas-ng</a>.</p><p>You logged in as <strong>%s</strong>.</p><p><a href="/accounts/logout">Logout</a></p>' % request.user)
        return HttpResponse('<p>Welcome to <a href="">django-cas-ng</a>.</p><p><a href="/accounts/login">Login</a></p>')

Generate database tables

$ python migrate
Operations to perform:
  Apply all migrations: admin, auth, contenttypes, django_cas_ng, sessions
Running migrations:
  Applying contenttypes.0001_initial... OK
  Applying auth.0001_initial... OK
  Applying admin.0001_initial... OK
  Applying admin.0002_logentry_remove_auto_add... OK
  Applying admin.0003_logentry_add_action_flag_choices... OK
  Applying contenttypes.0002_remove_content_type_name... OK
  Applying auth.0002_alter_permission_name_max_length... OK
  Applying auth.0003_alter_user_email_max_length... OK
  Applying auth.0004_alter_user_username_opts... OK
  Applying auth.0005_alter_user_last_login_null... OK
  Applying auth.0006_require_contenttypes_0002... OK
  Applying auth.0007_alter_validators_add_error_messages... OK
  Applying auth.0008_alter_user_username_max_length... OK
  Applying auth.0009_alter_user_last_name_max_length... OK
  Applying auth.0010_alter_group_name_max_length... OK
  Applying auth.0011_update_proxy_permissions... OK
  Applying django_cas_ng.0001_initial... OK
  Applying sessions.0001_initial... OK

You can also get the entire project source code at django-cas-ng/example. The main change is commit 4dea11de05.

Run test server

$ python runserver
Performing system checks...

Django version 3.0.2, using settings 'mysite.settings'
Starting development server at
Quit the server with CONTROL-C.

To play to this demo project, you can simply run following commands to clone the code and run your local server instance:

git clone
cd example
pip install -r requirements.txt
python runserver

Live Demo

If you want to see a live demo, you can click here. The following username/password can be used to login in demo server.

username: admin
password: django-cas-ng

Demo CAS Server

To make the entire flow work, I also created a demo CAS server to work with this example project, open source at demo-cas-server.