title: Async View in Django
body: <p>Working on a django app that depends on a payment API, the app has to redirect based on response from user's phone input which takes a while, is there a way to use <code>async</code> to wait for the user's input before running the rest of the code.
  I would like for the code to run only once it gets the <code>response</code> as shown in the function below.
  Any help would be appreciated.</p>
  <pre><code>class CallBack(TemplateView):
  
  template_name = 'callback.html'
  
  def get(self, request):
  
      response = json.loads(request.body.decode('utf-8'))
  
      result_code = response[&quot;Body&quot;][&quot;stkCallback&quot;][&quot;ResultCode&quot;]
      
      if result_code == 0:
          merchant_request_id = response[&quot;Body&quot;][&quot;stkCallback&quot;][&quot;MerchantRequestID&quot;]
          checkout_request_id = response[&quot;Body&quot;][&quot;stkCallback&quot;][&quot;CheckoutRequestID&quot;]
          result_code = response[&quot;Body&quot;][&quot;stkCallback&quot;][&quot;ResultCode&quot;]
          amount = response[&quot;Body&quot;][&quot;stkCallback&quot;][&quot;CallbackMetadata&quot;][&quot;Item&quot;][0][&quot;Value&quot;]
          mpesa_receipt_number = response[&quot;Body&quot;][&quot;stkCallback&quot;][&quot;CallbackMetadata&quot;][&quot;Item&quot;][1][&quot;Value&quot;]
          transaction_date = response[&quot;Body&quot;][&quot;stkCallback&quot;][&quot;CallbackMetadata&quot;][&quot;Item&quot;][2][&quot;Value&quot;]
          phone_number = response[&quot;Body&quot;][&quot;stkCallback&quot;][&quot;CallbackMetadata&quot;][&quot;Item&quot;][3][&quot;Value&quot;]
          str_transaction_date = str(transaction_date)
          transaction_datetime = datetime.strptime(
              str_transaction_date, &quot;%Y%m%d%H%M%S&quot;)
          aware_transaction_datetime = pytz.utc.localize(
              transaction_datetime)
          
          our_model = Mpesa.objects.create(
              CheckoutRequestID=checkout_request_id,
              MerchantRequestID=merchant_request_id,
              PhoneNumber=phone_number,
              MpesaReceiptNumber=mpesa_receipt_number,
              Amount=amount,
              ResultCode=result_code,
              TransactionDate=aware_transaction_datetime,
          )
          our_model.save()
          
          return redirect('realtor:create')
      else:
          return redirect('payments:lipa_na_mpesa')
  </code></pre>