Django REST framework is a powerful and flexible toolkit for building Web APIs. Some reasons you might want to use REST framework: The Web browsable API is a huge usability win for your developers. Authentication policies including packages for OAuth1a and OAuth2.
Let's begin our journey.... 🧑🏻✈️ 🧑🏻✈️ 🧑🏻✈️
I assume that you are already an experience with creating virtual Environment and setting up your projects. Your project structure seems like this.
GIT Repo link: pyJWT
What we cover here?
- Creating Custom user.
- Sign-in/sign up
- JWT Setup
- Serializations
- Testing with Postman
Installation & Setup:
For this tutorial we are going to use the pyJWT
library.
After installations completes, let's create a new app name accounts.
Let's register accounts app in settings.py file of main project.
Before we go further let's config our urls file. Create a urls.py file inside accounts app.
In our main app urls, add little code. which are as follow:
Okay, here we go to another round....😤💪 😤💪 😤 💪😤💪 😤 💪😤💪 😤
👊🏼👊🏼👊🏼👊🏼👊🏼👊🏼👊🏼👊🏼👊🏼👊🏼👊🏼 ROUND 1 BEGIN 👊🏼👊🏼👊🏼👊🏼👊🏼👊🏼👊🏼👊🏼👊🏼👊🏼👊🏼👊🏼👊🏼
Creating a Model
Let's create a custom user using AbstractBaseUser. There are two ways to create a custom user model in Django: AbstractUser
and
AbstractBaseUser
.
For more details : CLICK HERE, i won't be lecturing here.
What is ? and what is not ?..... Read yourself......... 😁😁😁😁😁😁😁😁
Our custom user model seems like this:
Let's create our base user manager.......
Overall, our codes looks like this:
so we need to do a little tweak to recognize our custom user by django, let's march to the main app of settings.py file. At the very bottom of file write this very code.
so in string quotes where we define accounts which means our very created app and MyUser refers to the custom user model which we created.
Before testing our app let's migrate to database.
Hope You guys made it ......
Okay, now we register our user models to admin.py file, in order to view on admin panel.... Let's march towards admin.py file of our accounts app...
admin.py file should looks like this:
So login and see magic......💯 [Note: actually it is not magic]