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.
But if you see the webpage, we don't have an option to logout and login as another user.
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.
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')),
And if you refresh the webpage you would be able to see a login button.
Now, click on login and login as the new user.
And try to upvote the post as gaurav, you shall she the below:
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.