0

I am trying to integrate django and DRF with firebase real-time database and while creating a post request I ran into a CSRF verification error.

CSRF verification failed. Request aborted.

Some solutions I found online said that I should use render but I am actually using Django Rest Framework and would like to use something that works with JsonResponse and Serializers. Here's a test code.

    def post(self, request):
        """Function to handle post requests

        Args:
            request (_type_): _description_
        """

        # Get the data to be posted from the request 
        name = request.POST.get('name')
        age = request.POST.get('age')
        location = request.POST.get('location')

        # Set the reference to the database
        ref = db.reference('/user1')

        # Push the data to the database
        ref.push({
            'Name': name,
            'Age': age,
            'Location': location
        })

Let me know if I should share how I am creating my firebase client, although I don't think that would be necessary. In conclusion, I would like to find a way to add a csrf_token to a post request method in Django and DRF.

Another solution I saw used csrf_exempt but I don't want to use that either as that is not ideal.

I do plan on taking this to production at some point so do recommend solutions keeping that in mind.

Prakhar Rathi
  • 905
  • 1
  • 11
  • 25

0 Answers0