Django
Django is a high-level Python Web framework that aids in quick development and clean, realistic design. It reduces the hassle so we can focus on coding the app rather than reinventing the whole wheel.

Let's Get your Django App running with SAWO
πŸ™Œ

​

Requirements

Python, pip (package-management system written in Python used to install and manage software packages)
Steps
1. The first step to getting started with SAWO API integration into your Django website/application is to install the sawo package as given below:
1
pip install sawo
Copied!
2. Next, you have to import createTemplate, getContext, and verifyToken methods from Sawo. The getContext method is used to create sawo dictionary object from data gain from Django Database. The ways to create the template and verify the token are given in subsequent steps.
1
from sawo import createTemplate, getContext, verifyToken
Copied!
3. To use SAWO Login you would need an API key which can be obtained by creating a project in the sawo dashboard.
4. Once you create your project, you would need to set your project name and hostname. 4.1. For development in a local machine, the hostname should be set to 'localhost'.
If using ''localhost" as hostname is not working for you, try "127.0.0.1"
4.2. For the production, the hostname should be set to your domain.
If you are adding your domain do not add 'https://', "http://', 'www' or even trailing backslash.
Example: https://dev.sawolabs.com/ should be kept as dev.sawolabs.com
5. Copy the API key from the project and keep it safe and secure.
The best practice to store your API key is to store values in .env so that they are not exposed.
6. Getting Started with the Setup in Django
    First, create a template for SAWO password-less and OTP-less Authentication on your Django website. Use the code given below for the same:
1
createTemplate("<filepath>")
2
#example
3
createTemplate("templates/partials")
Copied!
    Next, you have to send this data required by _sawo.html. The variable names used in _sawo.html template are sawo.auth_key, sawo.identifier and sawo.to Therefore, to send this data you have to create a JSON.
Note:
    The "to" route should be a post route that can receive posted data.
    If you are not aware of how data is passed to templates in Django, it is suggested to look into it first.
There are two methods to do this:
METHOD 1: SENDING STATIC DATA
1
context = {
2
"sawo":{
3
"auth_key": "<api_key>",
4
"identifier": "email | phone_number_sms"
5
"to": <route> #the route where you will receive
6
the payload sent by sdk
7
}
8
}
9
​
10
#example
11
context = {
12
"sawo":{
13
"auth_key": "785ha-hdjsdsd-799-ss345",
14
"identifier": "email | phone_number_sms"
15
"to": login
16
}
17
}
Copied!
METHOD 2: USING ADMIN AND DATABASE TO SAVE CONFIG FOR SAWO
Step 1: To begin, the sawo api_key and an identifier have to be created from the admin dashboard. The following code can be used in the models of your app.
1
class Config(models.Models):
2
api_key = models.CharField(max_length=200)
3
identifier = models.CharField(max_length=200)
4
choices = [("email","Email"),("phone_number_sms","Phone")]
Copied!
Step 2: Next, the view.py of the app has to be set up. Use the code given below for the same.
Route should be the receiving end where you can handle post request
1
from models import Config
2
from sawo import getContext
3
​
4
def <your_function>(request):
5
config = Config.objects.order_by('-api_key')[:1]
6
context = {
7
"sawo" = getContext(config,<route>) #the route where you will receive
8
the payload sent by sdk
9
}
10
11
#example
12
​
Copied!
Once the SAWO SDK is successfully set up, a login form will be rendered in the provided container as displayed in the picture below:
Final Render of SAWO Login
Congratulations !! The SAWO API is now ready to be used in your Django application 🀘.

You can also check out SAWO's Django Sample Code and Deployed App.​

It's okay, we get it! You got Stuck!
😞
Feel free to contact us on #ask-for-help on our Discord​

Last modified 1mo ago
Copy link