Joseph Davis
Joseph Davis

Reputation: 118

PUT method is not working in django REST?

PUT method is not working in my django rest frame work.

Models.py

class Profile(models.Model):
    first_name = models.CharField(max_length=30)
    last_name = models.CharField(max_length=30,blank=True)

Corresponding view

class ProfileDetailsView(APIView):
    """
    Get, udpate,  a user's profile
    """

    template_name = 'users/profile.html'
    form_class = UserProfileForm
    authentication_classes = (SessionAuthentication,)


    def get_object(self, pk):
        try:
            return Profile.objects.get(pk=pk)
        except Profile.DoesNotExist:
            raise Http404

    def get(self, request, pk, format=None):
        print "111111111111111111"
        profile = self.get_object(pk)
        serializer = UserProfileSerializer(profile)
        return render(request, self.template_name, context=serializer.data)


    def put(self, request, pk):
        print "2222222222222222222"

When I request the page(GET), it displays the corresponding details(profile details). But when I submit the page(PUT) it still goes to get section instead of put. It works well when I use the REST view. The problem is with the web view(html).What I'm missing? Any help appreciated

HTML page

<form class="form-profile" >{% csrf_token %}
 <div class="form-horizontal">
  <div class="form-group"> 
   <label class="col-sm-2 control-label">First name</label>
   <div class="col-sm-10">
    <input value="{{first_name}}" class="form-control" id="first_name" maxlength="255" name="first_name" placeholder="First name" required="required" type="text" />
   </div>
  </div> 
  <div class="form-group"> 
   <label class="col-sm-2 control-label">Last name</label>
    <div class="col-sm-10">
     <input value="{{last_name}}" class="form-control" id="last_name" maxlength="255" name="last_name" placeholder="First name" type="text" />
    </div>
  </div>
 </div>
</form>

Upvotes: 1

Views: 1832

Answers (1)

bakkal
bakkal

Reputation: 55448

But when I submit the page(PUT) it still goes to get section instead of put

I don't think a standard HTML form can use PUT, it's either GET (the default) or POST. Here's quote from the HTML5 spec:

The method and formmethod content attributes are enumerated attributes with the following keywords and states:

The keyword get, mapping to the state GET, indicating the HTTP GET method.

The keyword post, mapping to the state POST, indicating the HTTP POST method.

The missing value default for these attributes is the GET state.

Other than that if you want to use POST, then specify it in the form

<form class="form-profile" method="post">

I found this post you might like reading it: Why are there are no PUT and DELETE methods on HTML forms?

Of course you can use JavaScript to collect the data from the form and initiate a PUT request but that's not the standard HTML form you seem to be going for.

Here's some ways you can send PUT requests to your DRF

Upvotes: 3

Related Questions