1

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>
Joseph Davis
  • 118
  • 1
  • 14

1 Answers1

3

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

Community
  • 1
  • 1
bakkal
  • 54,350
  • 12
  • 131
  • 107
  • This will be correct in case of HTML. But my question is how can I do with Django? PUT request in django with REST framework or else how can I update profile details – Joseph Davis Feb 15 '16 at 15:20
  • Depends on what you'd like to use, virtually any HTTP client will be able to do it. I included links to one with jQuery for your web page and one with curl, for your command line – bakkal Feb 15 '16 at 15:23
  • I think I see your problem a bit better now, it's a mismatch between how some RESTful APIs (e.g. a standard ModelViewSet in DRF) uses POST just to create, and PUT to update, while a Django HTML offers no such distinction. So usually if you want to keep the HTML form, you usually read the ID (primary key) of the submitted object from the form, if that ID exists in the form, then it's an update. If it is blank then it is a create. I hope this makes to you. Otherwise, your APIView is free to use a POST for both operations, just read the ID/PK params. – bakkal Feb 15 '16 at 15:26
  • Without jQuery?, means normal PUT method. Like I said earlier I can do it with REST console with the same code, so is there any way to do it without jQuery? – Joseph Davis Feb 15 '16 at 15:29
  • Without jQuery sure, anything that can generate a XHR request, so minimum is some JS. Since we agreed a HTML form cannot initiate a PUT. – bakkal Feb 15 '16 at 15:30
  • FWIW, Django REST Framework's Web Browsable API uses JS and jQuery to accomplish that: https://github.com/tomchristie/django-rest-framework/tree/master/rest_framework/static/rest_framework/js So Whatever way you think you accomplished that without it isn't correct, an HTML form doesn't submit with a PUT. – bakkal Feb 15 '16 at 15:35
  • Okay Thanks. I don't know exactly if the REST console is already equipped with JS or jQuery, but it works. – Joseph Davis Feb 15 '16 at 15:59