# Django application integrate with keycloak

**URL:** <https://forum.keycloak.org/t/django-application-integrate-with-keycloak/16437>\
**Category:** Getting advice\
**Tags:** authentication, oidc, saml\
**Created:** [July 13, 2022, 1:56pm UTC](https://forum.keycloak.org/t/django-application-integrate-with-keycloak/16437 "2022-07-13T13:56:21Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![rhussain](https://avatars.discourse-cdn.com/v4/letter/r/ba8739/32.png) [@rhussain](https://forum.keycloak.org/u/rhussain)\
**Post date:** [July 13, 2022, 1:56pm UTC](https://forum.keycloak.org/t/django-application-integrate-with-keycloak/16437/1 "2022-07-13T13:56:21Z")

</div>

Hi,  
I have Django application which is using a djangorestframework to create restful api. I would like to integrate user system for authentication with keycloak. I am not getting anywhere, can anybody please give me advise, how to integrate authenticate system with keycloak.

Thanks in advance

---

<div class="post-metadata">

**Author:** ![abdelbassetsadok](https://avatars.discourse-cdn.com/v4/letter/a/b5e925/32.png) [@abdelbassetsadok](https://forum.keycloak.org/u/abdelbassetsadok)\
**Post date:** [March 5, 2023, 5:31pm UTC](https://forum.keycloak.org/t/django-application-integrate-with-keycloak/16437/2 "2023-03-05T17:31:17Z")

</div>

hello @rhussain did you figure out a way to do this please??

---

<div class="post-metadata">

**Author:** ![xgp](https://yyz2.discourse-cdn.com/free1/user_avatar/forum.keycloak.org/xgp/32/2589_2.png) [@xgp](https://forum.keycloak.org/u/xgp)\
**Post date:** [March 5, 2023, 9:00pm UTC](https://forum.keycloak.org/t/django-application-integrate-with-keycloak/16437/3 "2023-03-05T21:00:33Z")

</div>

The Mozilla Django OIDC [https://mozilla-django-oidc.readthedocs.io/](https://mozilla-django-oidc.readthedocs.io/) library can facilitate this.

---

<div class="post-metadata">

**Author:** ![abdelbassetsadok](https://avatars.discourse-cdn.com/v4/letter/a/b5e925/32.png) [@abdelbassetsadok](https://forum.keycloak.org/u/abdelbassetsadok)\
**Post date:** [March 10, 2023, 2:38pm UTC](https://forum.keycloak.org/t/django-application-integrate-with-keycloak/16437/4 "2023-03-10T14:38:06Z")

</div>

@xgp thank you for you reply, but i’m wondering if you have an exemple or repository which uses this library to integrate kc

---

<div class="post-metadata">

**Author:** ![xgp](https://yyz2.discourse-cdn.com/free1/user_avatar/forum.keycloak.org/xgp/32/2589_2.png) [@xgp](https://forum.keycloak.org/u/xgp)\
**Post date:** [March 10, 2023, 5:28pm UTC](https://forum.keycloak.org/t/django-application-integrate-with-keycloak/16437/5 "2023-03-10T17:28:12Z")

</div>

[https://www.google.com/search?q=django+oidc+keycloak+example](https://www.google.com/search?q=django+oidc+keycloak+example)

---

<div class="post-metadata">

**Author:** ![abdelbassetsadok](https://avatars.discourse-cdn.com/v4/letter/a/b5e925/32.png) [@abdelbassetsadok](https://forum.keycloak.org/u/abdelbassetsadok)\
**Post date:** [March 16, 2023, 2:42pm UTC](https://forum.keycloak.org/t/django-application-integrate-with-keycloak/16437/6 "2023-03-16T14:42:39Z")

</div>

i’m looking for a complete project with django bcz all the project are not comlted or not working @xgp

---

<div class="post-metadata">

**Author:** ![xgp](https://yyz2.discourse-cdn.com/free1/user_avatar/forum.keycloak.org/xgp/32/2589_2.png) [@xgp](https://forum.keycloak.org/u/xgp)\
**Post date:** [March 16, 2023, 3:33pm UTC](https://forum.keycloak.org/t/django-application-integrate-with-keycloak/16437/7 "2023-03-16T15:33:58Z")

</div>

Please share what you have tried and what is not working. Perhaps we can help you debug.

---

<div class="post-metadata">

**Author:** ![abdelbassetsadok](https://avatars.discourse-cdn.com/v4/letter/a/b5e925/32.png) [@abdelbassetsadok](https://forum.keycloak.org/u/abdelbassetsadok)\
**Post date:** [March 16, 2023, 3:52pm UTC](https://forum.keycloak.org/t/django-application-integrate-with-keycloak/16437/8 "2023-03-16T15:52:05Z")

</div>

thank you @xgp i tried to use this repo

> **[GitHub - thiagonf/djangoSSO: Repository of a Django application...](https://github.com/thiagonf/djangoSSO)**
>
> Repository of a Django application authenticating in Keycloak - GitHub - thiagonf/djangoSSO: Repository of a Django application authenticating in Keycloak

it has some docs which i followed step by step:

> **[Keycloak Server](https://github.com/thiagonf/djangoSSO/wiki/Keycloak-Server)**
>
> Repository of a Django application authenticating in Keycloak - thiagonf/djangoSSO

> **[Django Keycloak](https://github.com/thiagonf/djangoSSO/wiki/Django-Keycloak)**
>
> Repository of a Django application authenticating in Keycloak - thiagonf/djangoSSO

i followed the steps evrything was okey, but when i tried to login via /login it gives me a redirect errors like this screenshoots:

 ![first](https://global.discourse-cdn.com/free1/uploads/keycloak/original/2X/7/7dbb3cb461abe467208338ad6a952545dc9d5285.png)  
 ![second](https://global.discourse-cdn.com/free1/uploads/keycloak/original/2X/7/77b036f3a1ecf8b964d389eb3b3848976ef7d9e5.png)

---

<div class="post-metadata">

**Author:** ![xgp](https://yyz2.discourse-cdn.com/free1/user_avatar/forum.keycloak.org/xgp/32/2589_2.png) [@xgp](https://forum.keycloak.org/u/xgp)\
**Post date:** [March 16, 2023, 4:10pm UTC](https://forum.keycloak.org/t/django-application-integrate-with-keycloak/16437/9 "2023-03-16T16:10:00Z")

</div>

The `redirect_uri` you set when you set up the Client in Keycloak must match the one the browser is setting.

---

<div class="post-metadata">

**Author:** ![abdelbassetsadok](https://avatars.discourse-cdn.com/v4/letter/a/b5e925/32.png) [@abdelbassetsadok](https://forum.keycloak.org/u/abdelbassetsadok)\
**Post date:** [March 16, 2023, 4:24pm UTC](https://forum.keycloak.org/t/django-application-integrate-with-keycloak/16437/10 "2023-03-16T16:24:22Z")

</div>

@xgp what di you lean by the browser is settings,knowing that for now i’m using the templates of django

---

<div class="post-metadata">

**Author:** ![xgp](https://yyz2.discourse-cdn.com/free1/user_avatar/forum.keycloak.org/xgp/32/2589_2.png) [@xgp](https://forum.keycloak.org/u/xgp)\
**Post date:** [March 16, 2023, 4:30pm UTC](https://forum.keycloak.org/t/django-application-integrate-with-keycloak/16437/11 "2023-03-16T16:30:09Z")

</div>

It’s probably the URL of the page before you click on “Sign In”. Open the Developer Tools in your browser and choose the Network tab. After you click on “Sign In”, look for a request to the Keycloak server that contains a request with a `redirect_uri` parameter in the request. That will tell you what the browser is setting it to. Then you have to make sure that is in the “Valid redirect URIs” section of the Client you set up in Keycloak.

---

<div class="post-metadata">

**Author:** ![rcapp](https://yyz2.discourse-cdn.com/free1/user_avatar/forum.keycloak.org/rcapp/32/7705_2.png) [@rcapp](https://forum.keycloak.org/u/rcapp)\
**Post date:** [March 16, 2023, 8:36pm UTC](https://forum.keycloak.org/t/django-application-integrate-with-keycloak/16437/12 "2023-03-16T20:36:16Z")

</div>

Hello! We are implementing Keycloak on our Django application, over Django REST Framework (DRF).

There are a few things I’d like to point out:

- If you are going to work with Session and want to integrate your **Django users** with Keycloak (KC), **mozilla-django-oidc** will work and it is the easiest and better one from all the libraries we configured.

- If you are going to work with APIs and but **not** with Django users, only authenticating in KC, you should use [Python Keycloak](https://github.com/marcospereirampj/python-keycloak). Python KC implements most of the Keycloak REST APIs, which will save you from doing a lot of hand work to put up methods and functions.

Their documentation aren’t that extensive, but you can figure out most things reading their code and cross referencing with KC REST docs [1](https://www.keycloak.org/docs-api/21.0.1/rest-api/index.html "latest").

- If you are going to work both with Browser Flow (sessions, I believe) and API authentication/authorization via tokens, I cannot help much because we are only integrating it with tokens. But I believe Python KC can suffice this too.

I’m not sure if Python KC has everything from the new API implemented, so double check both 21 [1](https://www.keycloak.org/docs-api/21.0.1/rest-api/index.html "latest") and 20 [2](https://www.keycloak.org/docs-api/20.0.3/rest-api/) versions. We are based in KC 20.

Cheers!
