Search code examples
pythondjangopostdjango-rest-frameworkdjango-rest-viewsets

How to make the django rest framework POST button work?


I have a model like this:

class Ad(models.Model) :
    title = models.CharField(
            max_length=200,
            validators=[MinLengthValidator(2, "Title must be greater than 2 characters")]
    )
    price = models.DecimalField(max_digits=7, decimal_places=2, null=True)
    text = models.TextField()
    
    
    # Shows up in the admin list
    def __str__(self):
        return self.title

A Viewset like this:

class AdViewSet(viewsets.ModelViewSet):
    """
    API endpoint that allows ad to be viewed or edited.
    """
    queryset = Ad.objects.all()
    serializer_class = AdSerializer
class AdSerializer(serializers.ModelSerializer):
    class Meta:
        model = Ad
        fields = ['id', 'title', 'price', 'text']

I have made the router that I didn't include here because I don't think it's needed to show my issue.

In the Django Rest Framework in /api/ads/ I can see all my previous ads created using the django admin and below a form to post data which looks like this:

enter image description here

When I click on the POST button of the form,it doesn't make a POST request it just reload the page. I can confirm it from the Firefox devtool and the server log, no POST request are made.

So my question is this: how to make this POST button works as expected so I can check my serialiser and everything work as it should ?

Edit: After testing, put, patch, and delete button also don't work, they don't send put/patch/delete header, just get header like post


Solution

  • After a lot of research I found the issue was with ublock origin.

    Ublock origin was preventing the browser from making request on localhost so I deactivated it and now it work as expected.

    Check your adblocker if you have the same issue.