title: Django and post request to an external API in different views
body: <p>So i want to create a Django App where my users can post data through a form and make a post request to an external API, but getting the response in the same page/view</p>
  
  <p>For example, i have my view</p>
  
  <pre><code>class Home(TemplateView):
      template_name: 'home/index.html'
  </code></pre>
  
  <p>And i have my index.html:</p>
  
  <pre><code>&lt;form id="formdata" &gt;
      &lt;select id="options"&gt;
          &lt;option id="sku"&gt;Option 1&lt;/option&gt;
          &lt;option id="sku2"&gt;Option 2&lt;/option&gt;
      &lt;/select&gt;    
  
      &lt;input name="number" type="text"  id="number"&gt;
  
      &lt;select id="price"&gt;
          &lt;option id="price1"&gt;$5&lt;/option&gt;
          &lt;option id="price2"&gt;%10&lt;/option&gt;
      &lt;/select&gt;    
  
      &lt;button type="button" data-loading-text="enviando..." onclick="submitInfo()"&gt;Send&lt;/button&gt;
  &lt;/form&gt;
  </code></pre>
  
  <p>Let's ignore the fact HTML may be wrong, it is a basic structure of a form with selects and input field, but note that i need to pass "product", "number" and "price" as parameters in the post request.</p>
  
  <p>The thing is that when the user clics on the submit button, they make a post request to an external api, i know i can do int with JavaScript using fetch, but the thing is that i need to pass my personal Token Key in the body params, also i'd like to hide the real api url hiding it with an url of my website, for example: www.myurl.com/my-api-call</p>
  
  <p>So i'm thinking about creating a "external_api_view" with post request, something like this: </p>
  
  <pre><code>import requests
  import time
  from rest_framework import status
  from rest_framework.response import Response
  
  def external_api_view(request):
      if request.method == "POST":
          attempt_num = 0  # keep track of how many times we've retried
          while attempt_num &lt; MAX_RETRIES:
              url = 'www.apiexternal.com/endpoint'
              payload = {'Token':'My_Secret_Token','product':'product_select_in_form','price':'price_selected_in_form'}
              response = requests.post(url, data = payload)
              if r.status_code == 200:
                  data = r.json()
                  return Response(data, status=status.HTTP_200_OK)
              else:
                  attempt_num += 1
                  # You can probably use a logger to log the error here
                  time.sleep(5)  # Wait for 5 seconds before re-trying
          return Response({"error": "Request failed"}, status=r.status_code)
      else:
          return Response({"error": "Method not allowed"}, status=status.HTTP_400_BAD_REQUEST)
  </code></pre>
  
  <p>But not i have the problem that i don't know how to pass inputs of the form into that view, so i can make the post request and get the response through javascript on my index.html file (adding obviously the javascript needed)</p>
  
  <p>I don't even know if this is possible, i was thinking in doing something like so with rest framework, but also i have no idea how to</p>
  
  <p>Any help would be really appreciated :)</p>