0

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

BoumTAC
  • 3,531
  • 6
  • 32
  • 44
  • If you post the form in your pichture as shown you are just sending empty data. Have you tried using the HTML Form? – Chris Jan 04 '22 at 10:20
  • @Chris Sorry I update the image with real data. And confirm it again with real data. No post request have been send by the browser and no post request have been received by the server – BoumTAC Jan 04 '22 at 10:32
  • The only issue I can see from the code pasted is that ```created_at``` is not field in your model. Took that out and tried your code and it worked. However, you have not posted the code of your ```AdSerializer``` so it cannot ver checked whether there is an issue there. – Chris Jan 04 '22 at 10:50
  • The issue is that you do not have a `create` method in your `AdViewSet` so that POST request is not going to get processed. You need to get the data from the serializer and save it to the database in the `create` method. – Yusuf Ertas Jan 04 '22 at 12:17

1 Answers1

0

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.

BoumTAC
  • 3,531
  • 6
  • 32
  • 44