Creating Python APIs - The Django REST framework. Building a Reddit clone - API auth.[7/n]

In this article, we will be trying to test if our upvote functionality is really working by creating another user and having him upvote the same post.

You can create a new user from the Django admin panel.

image.png

But if you see the webpage, we don't have an option to logout and login as another user.

image.png

And if you try to login through the admin panel, it won't let you in as the account we created is not an admin account.

image.png

So what we can do is we can add a login feature to our API. It's a nifty feature of Django REST framework.

We just need to add the following path in the urls.py file:

path('api-auth', include('rest_framework.urls')),

image.png

And if you refresh the webpage you would be able to see a login button.

image.png

Now, click on login and login as the new user.

image.png

And try to upvote the post as gaurav, you shall she the below:

image.png

Now if you try to upvote the same post again you would receive the ValidationError that we created, so the code works perfectly.

Now let's go to the admin panel to check if the data is properly added to the database. We can see our new users upvote.

image.png

No Comments Yet