How can I make a Django REST register/login routes available for Postman

Viewed 143

I have a simple Django app with a model called Person:

class Person(models.Model):
    user = models.ForeignKey(

     settings.AUTH_USER_MODEL,

     on_delete=models.CASCADE

   )

    birthday = models.DateField(null=True)

    gender = models.CharField(max_length=10)

    height = models.CharField(max_length=3)

    weight = models.CharField(max_length=3)

I have also a User -model like this:

from django.contrib.auth.models import AbstractUser

class CustomUser(AbstractUser):

    pass

    # add additional fields in here

    def __str__(self):

        return self.username

I can log in in the admin panel and create new users from there and link my Person -model with a User. What I'm wondering is how can I define for example register/ and login/ routes so I could for example register a new user with Postman? How about authentication, could someone give a simple example how to serve right data to right users?

1 Answers

you need to add your views to urls.py and link the URLs with their views.

from django.urls import path
from <your_view_file> import views

urlpatterns = [
path('login', views.<your_login_view>)
]

Very basically assignment between URLs and functions is like this. Of course, you need to write your register and login functions. Check here for a tutorial, it includes authentication basics, too. Also, I recommend DRF, so check this link for a basic understanding of authentication and permissions.

Related