• Register
2 votes
19 views

Problem:

I am new in django I was trying some code to get and post some data. I wrote a code:
from rest_framework.views import APIView
from rest_framework.response import Response
from rest_framework import status
from rest_framework import viewsets


from profiles_api import serializers


class HelloApiView(APIView):
    """Test API View"""
    serializer_class = serializers.HelloSerializer
    def post(self, request):
        """Create a hello message with our name"""
        serializer = self.serializer_class(data=request.data)

        if serializer.is_valid():
            name = serializer.validated_data.get('name')
            message = f'Hello {name}!'
            return Response({'message': message})
        else:
            return Response(
                serializer.errors,

But every time I run my server It shows an error message:

message HTTP method POST is not supported by this URL
description The specified HTTP method is not allowed for the requested resource (HTTP method POST is not supported by this URL)

 

Can anyone help?

10 7 2
6,060 points

Please log in or register to answer this question.

1 Answer

0 votes

Answer:

For your api you need to call a get method then post or else It would show this error message. So write your code like:

from rest_framework.views import APIView
from rest_framework.response import Response
from rest_framework import status
from rest_framework import viewsets


from profiles_api import serializers


class HelloApiView(APIView):
    """Test API View"""
    serializer_class = serializers.HelloSerializer

    def get(self, request, format=None):
        """Returns a list of APIView features"""
        an_apiview = [
            'Uses HTTP methods as function (get, post, patch, put, delete) ',
            'Is similar to a tarditional Django View',
            'Gives you the most control over your application logic',
            'Is mapped manually to URLs',
        ]

        return Response({'message': 'Hello!', 'an_apiview': an_apiview })

    def post(self, request):
        """Create a hello message with our name"""
        serializer = self.serializer_class(data=request.data)

        if serializer.is_valid():
            name = serializer.validated_data.get('name')
            message = f'Hello {name}!'
            return Response({'message': message})
        else:
            return Response(
                serializer.errors,
                status=status.HTTP_400_BAD_REQUEST
            )

Happy coding

11 6 4
34,950 points

Related questions

0 votes
1 answer 20 views
20 views
Problem: I am a na&iuml;ve to Java, Jsp, Servlet and trying to make a very small registration program with the database. But while trying to run this I am facing below errors: HTTP Status 405 - HTTP method GET is not supported by this URL type Status report message HTTP ... </welcome-file>     </welcome-file-list> </web-app> I am looking for Java expert who can help me in fixing above error.
asked Jul 29, 2020 Raphael Pacheco 4.9k points
0 votes
1 answer 749 views
749 views
Problem : Currently I am trying to add the table to the newly created database through SQL Server Management Studio. But I get the below error: &ldquo;The backend version is not supported to design database diagrams or tables&rdquo; I am very new to SQL Server Management Studio. How can I resolve the above error?
asked Jan 28, 2020 jwilliam 3.9k points
0 votes
1 answer 51 views
51 views
Problem: I am new and learning, I need help, can anyone help by filling in the blanks? A field in an Access database table that has no value is determined by Access to have a ____ value? A. custom B. zero C. null D. number
asked Feb 21, 2020 maddi86 5.4k points
0 votes
1 answer 45 views
45 views
Problem: Django I have the following models: class Group(models.Model): name = models.CharField(max_length=100) parent_group = models.ManyToManyField("self", blank=True) def __unicode__(self): return self.name class Block(models.Model): name = models.CharField(max_length= ... return blocks But b1.group.block_set returns me a RelatedManager object that cannot be repeated. What to do? What's wrong?
asked Dec 13, 2020 sasha 5.9k points
0 votes
1 answer 23 views
23 views
Problem: In this logic structure, one program statement follows another?
asked Apr 12, 2020 ArifulIslam 5.6k points
0 votes
1 answer 8 views
8 views
Cannot open database requested by the login I can not log in to a database in SQL Server. Can you suggest a solution?
asked Nov 11, 2020 miki 1.9k points
0 votes
1 answer 127 views
127 views
Problem: A(n) _____ might be written by a programmer or it might be created through a DBMS utility program?
asked Jul 23, 2020 conner 1.5k points
0 votes
1 answer 14 views
0 votes
1 answer 12 views
12 views
Problem: There is a database on postgresql. There is a Django script that collects data. The problem is that there is a LOT of data. The table, with which it is necessary to operate, contains, according to conservative estimates, from hundreds of millions ... {}'.format(str(self.origination_host), str(self.termination_host)) class Meta: verbose_name = 'CDR data' verbose_name_plural = 'CDR data'
asked Dec 7, 2020 sasha 5.9k points